【问题标题】:json.net parsing windows phonejson.net解析windows phone
【发布时间】:2011-06-29 22:54:07
【问题描述】:

我向服务器发出请求并得到响应

    {"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]},

我正在尝试将其解析为列表框(不是现在,而是将来),并且我想获得一个带有类似

的字符串的数组
string[] names=["Vlad","Max","Sofi"]
string[] uids=["18186153","22285147","22532029"]

这是我不工作的代码 =(。我正在为 WP7 使用 Json.net

    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);
                    }*/

                }

我知道该怎么做,但不知道怎么做。 我有一个包含键值的字典,并且需要将每个字典收集到像

这样的数组中
for(int i=0;i<dictionary.items;i++)
{
    if dictionary.item.key[i]=['first_name']
        string_array_first_names[i]=dictionary.item.key[i];
}

【问题讨论】:

    标签: .net parsing windows-phone-7 json.net


    【解决方案1】:

    我发现在使用 JSON 时,最好在代码中创建对象结构并加载它,你正在做的子字符串是有风险的,并且在 6 个月后很难理解。 从那里您可以使用 LINQ 来获取您认为需要的任何数组或字典。

    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; }
    }
    

    【讨论】:

    • 我认为第二行应该是: var response = JsonConvert.DeserializeObject>(json);
    • 问题中的 JSON 以 {"response":[{..如果是的话我会更新的。
    • 我有一些问题:“用户”未定义,字典标记为未知定义(未找到类型或命名空间),
    • 更新:我有一些问题:“用户”(“对象”不包含“用户”的定义,并且找不到接受“对象”类型的第一个参数的扩展方法“用户” ,您是否缺少参考...”)字典标记为未知定义(未找到类型或命名空间),或者如果我添加“使用 System.Collections; using System.Collections.Generic;”它说“错误 3 使用泛型类型 'System.Collections.Generic.Dictionary' 需要 2 个类型参数”
    • @wsevendays bah,我的代码 sn-p 中的所有通用参数都被转换为 HTML。也许这就是第一个评论者注意到的。查看更新后的代码 sn-p。
    【解决方案2】:
    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"]);
                        }
    

    【讨论】:

      【解决方案3】:
      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());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-22
        相关资源
        最近更新 更多