2010-11-16 61 views
4

我有了消息数组的JSON结果:如何解析Windows Phone 7的以下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?我看到你也问过这里(http://stackoverflow.com/questions/4197646/how-can-i-use-the-jsonobject-class),但它仍然没有意义的WP7没有第三方库。 – 2011-10-16 19:00:54

回答

2
var list = messages["messages"]; 

我想也许你messages变量重命名为data更描述。

P.S.我从来没有见过我的姓氏作为名字!

+2

现在,如果只有你的名字是阿拉姆... – Sathya 2010-11-16 19:45:51

+0

我不认为我仍然明白。什么是var列表包含?我怎样才能从'消息'中提取'消息'块?最终我想填充一个列表框 – 2010-11-17 00:21:04

+1

它将包含一个继承自IEnumerable 的JsonArray,因此您可以循环使用它 – 2010-11-17 03:15:06

2

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

您不需要使用Encoding.UTF8.GetBytesMemoryStream

然后您可以从@John声明的JSON对象内部获取数据。

+0

我仍然不熟悉如何获取json对象中的数据,如@John所述。你能告诉我一个例子吗? – 2010-11-17 19:39:09