Reputation: 3768
I make a request to server and get response
{"response":[{"uid":18186153,"first_name":"Vlad","last_name":"Prylipko","online":0,"lists":[1]},
{"uid":22285147,"first_name":"Max","last_name":"Apro","online":0,"lists":[1]},
{"uid":22532029,"first_name":"Sofi","last_name":"Cei","online":0,"lists":[1]},
I'm trying to parse it into listbox(not now, but in future) and I want to get an array with strings like
string[] names=["Vlad","Max","Sofi"]
string[] uids=["18186153","22285147","22532029"]
Here is my not working code =(.I'm using Json.net for WP7
void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
lock (this)
{
string xx = e.Result;
string result = xx.Substring(13,xx.Length-15);
// JOb
ject o = JObject.Parse(xx);
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
foreach (string name in values.Keys)
{
Debug.WriteLine(name);
}
/*JDi z = JArray.Parse(xx);
for (int i = 0; i < 50; i++)
{
var list = z["first_name"][i];
// MessageBox.Show(list.ToString());
Debug.WriteLine(list.ToString());
}
/* for (int i = 0; i < o.; i++)
{
string name = (string)o[i]["first_name"];
Debug.WriteLine(name);
}*/
}
I know what to do, but don't know how to. I have a dictionary with key-values and need for each of them collect it into arrays like
for(int i=0;i<dictionary.items;i++)
{
if dictionary.item.key[i]=['first_name']
string_array_first_names[i]=dictionary.item.key[i];
}
Upvotes: 0
Views: 990
Reputation: 3768
for (int i = 0; i < count_friends; i++)
{
string photo_url = response["response"][i]["photo_medium_rec"].ToString();
usr[i].Uid = (int)response["response"][i]["uid"];
usr[i].First_Name = response["response"][i]["first_name"].ToString();
usr[i].Last_Name = response["response"][i]["last_name"].ToString();
usr[i].photo_url = photo_url;
lcst[i].bitmp = ImageCacher.ImageCacher.GetCacheImage(photo_url);
lcst[i].url = photo_url;
usr[i].isonline = Convert.ToBoolean((int)response["response"][i]["online"]);
}
Upvotes: 1
Reputation: 3768
string json = e.Result;
var response = JObject.Parse(json);
var getcount = response["response"].Children<JObject>();
int count_friends=getcount.Cast<JToken>().Values("uid").Count();
Response rr = new Response();
for (int i = 0; i <count_friends; i++)
{
// rr.Users.ToDictionary(rr.Users[i].Uid => response["response"][i]["uid"].ToString());
rr.Users[i].First_Name = response["response"][i]["first_name"].ToString();
// Debug.WriteLine("OUT: "+(string)response["response"][i]["uid"].ToString());
}
Upvotes: 0
Reputation: 9346
I find when working with JSON it is always best to create the object structure in code and load it, the substring you are doing is risky and difficult to understand 6 months from now. From there you can user LINQ to get out whatever arrays or dictionaries you feel you need.
string json = e.Result;
var response = JsonConvert.DeserializeObject<Response>(json);
string[] names = response.Users.Select(d=> d.First_Name).ToArray();
string[] uids = response.Users.Select(d=> d.Uid).ToArray();
Dictionary<string, User> users = response.Users.ToDictionary(d=> d.First_Name);
Dictionary<string, string> usersById = response.Users.ToDictionary(d=> d.Uid, d=> d.First_Name);
public class Response {
public User[] Users {get; set;}
}
public class User {
public string Uid {get; set; }
public string First_Name {get; set; }
public string Last_Name {get; set; }
public int Online {get; set; }
public int[] Lists {get; set; }
}
Upvotes: 3