解析简单的JSON数据为windows phone

本文关键字:windows phone 数据 JSON 简单 | 更新日期: 2023-09-27 18:01:23

当尝试解析一些json数据时,我变得难住了,这主要是因为它比我以前处理过的更简单。JSON是这样的:

[ { "guid" : "782ba9b2-be03-4e1c-884c-a862a9d80bc7",
    "id" : 1016,
    "message" : "test",
    "tag" : "test",
    "username" : "asd"
  },
  { "guid" : "08aa59ca-af56-4a60-9304-e96a86d4832d",
    "id" : 1012,
    "message" : "test",
    "tag" : "test",
    "username" : "123"
  },
  { "guid" : "08aa59ca-af56-4a60-9304-e96a86d4832d",
    "id" : 1011,
    "message" : "test",
    "tag" : "test",
    "username" : "12"
  },
  { "guid" : "08aa59ca-af56-4a60-9304-e96a86d4832d",
    "id" : 1010,
    "message" : "test",
    "tag" : "test",
    "username" : "1"
  }
]

,这是我当前的代码:

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string json = e.Result;
                if (!string.IsNullOrEmpty(json))
                {
                    GroupMessages results = JsonConvert.DeserializeObject<GroupMessages>(json);
                    JObject jo = new JObject();
                    jo = JObject.Parse(e.Result);
                    List<GroupMessages> contentList = new List<GroupMessages>();
                    JArray items = (JArray)jo[""];
                    int count = 0;
                    while (count < items.Count)
                    {
                        GroupMessages content = new GroupMessages();
                        tcUsername = items[count]["username"].ToString();
                        tcMessage = items[count]["message"].ToString();
                        tcGuid = items[count]["guid"].ToString();
                        tcTag = items[count]["tag"].ToString();
                        content.username = tcUsername;
                        content.message = tcMessage;
                        content.guid = tcGuid;
                        content.tag = tcTag;
                        contentList.Add(content);
                        count++;
                    }
                    generalList.ItemsSource = contentList.ToList();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Data.ToString());
            }
        }

and i think我知道错误在哪里:

JArray items = (JArray)jo[""];

由于我曾经使用过的大多数json都是{"results": [info here]},我不确定该在这里放什么。留空会导致它不工作,所以我不确定我是如何解决这个问题的。

解析简单的JSON数据为windows phone

使用JToken作为部分结果

JToken jt = JToken.Parse(e.Result);
JArray items = (JArray)jt;

您将获得一个JSON数组,因此使用JArray。解析

//  jo = JObject.Parse(e.Result);
List<GroupMessages> contentList = new List<GroupMessages>();
JArray items = JArray.Parse(e.Result);