代码之家  ›  专栏  ›  技术社区  ›  Sheehan Alam

如何为WindowsPhone7解析以下JSON?

  •  4
  • Sheehan Alam  · 技术社区  · 15 年前

    我有一个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]);
                    }
                }
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   John Sheehan    15 年前
    var list = messages["messages"];
    

    我可能会把你的 messages 变量到 data 更具描述性。

    P、 我从来没有把我的姓当成名字!

        2
  •  2
  •   Peter Mortensen Pieter Jan Bonestroo    15 年前

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

    你不需要使用 Encoding.UTF8.GetBytes MemoryStream .

    然后可以从JSON对象内部获取数据,如@John所述。

        3
  •  1
  •   Sumith P D    13 年前

    使用 http://json2csharp.com/ 获取csharp对象并使用序列化/反序列化JSON数据。