如何在Windows Phone 7上解析以下JSON?

4

我有一个JSON结果,其中包含一组消息的数组:

{
  "messages": [
    {
      "message": {
        "for_user_login": null,
        "message_type": "normal",
        "twitter_in_reply_to_screen_name": null,
        "avatar_url": "http://a2.twimg.com/profile_images/82661470/marshallwithhatbig_normal.jpg",
        "created_at": "2010-11-16T18:50:33Z",
        "body": "Watch the Web 2.0 Summit Live on Video, for Free: http://me.lt/24mH (tickets cost $Ks, content is good)",
        "filtered": false,
        "future": false,
        "in_reply_to_user_login": null,
        "twitter_user_id": 818340,
        "updated_at": "2010-11-16T18:50:33Z",
        "user_login": "marshallk",
        "group_ids": null,
        "stock_ids": "8030",
        "twitter_created_at": "2010-11-16T18:50:27Z",
        "id": 2124647,
        "mention_ids": null,
        "twitter_in_reply_to_user_id": null,
        "platform_user_login": null,
        "twitter_status_id": 4607245530697728,
        "user_id": null,
        "for_user_id": null,
        "recommended": false,
        "private_relations": false,
        "investor_relations": false,
        "forex": false,
        "in_reply_to_user_id": null,
        "stock_symbols": "KS",
        "twitter_in_reply_to_status_id": null,
        "twitter_source": "<a href=\"http://rockmelt.com\" rel=\"nofollow\">RockMelt</a>",
        "chart": false,
        "in_reply_to_message_id": null,
        "message_source": "twitter"
      }
    }
}

这是我的代码:

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.Result)))
            {
                JsonObject messages = (JsonObject)JsonObject.Load(stream);
                MessageBox.Show(messages.ToString(), "Data Passed", MessageBoxButton.OK);
            }
        }
    }

我不确定如何提取嵌套在根JSON对象中的JSON对象?

我也尝试过,但没有成功:

 JsonObject jsonString = (JsonObject)JsonObject.Parse(e.Result);
            JsonArray messages = (JsonArray)jsonString["messages"]["message"];

            foreach (JsonObject message in messages)
            {
                foreach (string body in message.Keys)
                {
                    Debug.WriteLine(body);
                    Debug.WriteLine(message[body]);
                }
            }

1
关于windows-phone-7标签,JsonObject来自哪个命名空间/ DLL?我看到你也在这里提出了这个问题(https://dev59.com/3VHTa4cB1Zd3GeqPV-9e),但是在wp7中没有第三方库仍然不合理。 - Daniel Ballinger
3个回答

2

为什么不直接使用JsonObject.Parse(e.Result)

您不需要使用Encoding.UTF8.GetBytesMemoryStream

然后,您可以像@John所述那样从JSON对象内获取数据。


我仍然不熟悉如何获取@John所述JSON对象内的数据。你能给我一个例子吗? - Sheehan Alam

2
var list = messages["messages"];

我建议你将 messages 变量改名为 data,以便更具描述性。
附注:我从未见过我的姓作为名字!

我不认为我还理解。变量列表会包含什么?我如何从“messages”中提取“message”块?最终,我想填充一个列表框。 - Sheehan Alam
1
它将包含一个继承自IEnumerable<JsonValue>的JsonArray,因此您可以遍历它。 - John Sheehan
你能否提供一个循环遍历JsonArray并展示如何提取message块的代码片段呢? - Sheehan Alam

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接