Sheehan Alam
Sheehan Alam

Reputation: 60869

How can I parse the following JSON for Windows Phone 7?

I have a JSON result that has an array of messages:

{
  "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"
      }
    }
}

here is my code:

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);
            }
        }
    }

I'm not sure how I can pull out the JSON object that is nested inside the root JSON object?

I have also tried with no luck:

 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]);
                }
            }

Upvotes: 4

Views: 4541

Answers (3)

Sumith P D
Sumith P D

Reputation: 41

use http://json2csharp.com/ to get csharp object and used serialize/deserialize JSON data.

Upvotes: 1

Zain Shaikh
Zain Shaikh

Reputation: 6043

Why don't you directly use JsonObject.Parse(e.Result)?

You do not need to use Encoding.UTF8.GetBytes or MemoryStream.

And then you can get data from inside the JSON object as stated by @John.

Upvotes: 2

John Sheehan
John Sheehan

Reputation: 78104

var list = messages["messages"];

I would maybe rename your messages variable to data to be more descriptive.

P.S. I've never seen my last name as a first name!

Upvotes: 2

Related Questions