c# - 如何为 Windows Phone 7 解析以下 JSON?

标签 c# .net json windows-phone-7 arrays

我有一个包含消息数组的 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]);
                }
            }

最佳答案

var list = messages["messages"];

我可能会将您的 messages 变量重命名为 data 以更具描述性。

附言我从未见过我的姓氏作为名字!

关于c# - 如何为 Windows Phone 7 解析以下 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198021/

相关文章:

c# - 如何告诉 Autofixture 创建具有不同 ID 的对象?

.net - WPF:验证时绑定(bind)到不可为空的值

.net - OPC HRESULT E_FAIL 当连接到远程服务器时

c# - 拒绝访问 Kiosk 程序进程

c# - C# 4.0 的 ExpandoObject 是否支持基于原型(prototype)的继承?

c# - C++ 混合模式应用程序中的内存管理

.net - 在 WPF DataGrid 中绑定(bind) ComboBoxColumn 的 ItemsSource

java - 错误: expected END_ARRAY but was STRING - only on specific phones when using mobile internet

java - 使用Gson序列化公开字段时如何更改字段名称?

jquery - 使用 AngularJS 在输入字段中显示 json 数据