Search code examples
c#jsontwitterjson.net

Parse Json data with multiple detail from twitter


I get following output from twitter and I want to only the following details to be captured from the output.

  1. Screen Name
  2. Followers Count
  3. profile_image_url

The problem is the data is within the nested Json.

Twitter API output:

[{ "contributors" : null,
"coordinates" : null,
"created_at" : "Wed Jan 29 09:18:15 +0000 2014",
"favorite_count" : 0,
"favorited" : false,
"geo" : null,
"id" : 428457050109382657,
"id_str" : "428457050109382657",
"in_reply_to_screen_name" : null,
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"lang" : "en",
"place" : null,
"user" : { "contributors_enabled" : false,
    "follow_request_sent" : null,
    "followers_count" : 218,
    "id" : 609465835,
    "id_str" : "609465835",
    "is_translation_enabled" : false,
    "is_translator" : false,
    "lang" : "en",
    "listed_count" : 3,
    "location" : "Still alpha as hell.",
    "verified" : false
  }
}, 
{ "contributors" : null,
"coordinates" : null,
"created_at" : "Wed Jan 29 09:18:15 +0000 2014",
"favorite_count" : 0,
"favorited" : false,
"geo" : null,
"id" : 428457050109382657,
"id_str" : "428457050109382657",
"in_reply_to_screen_name" : null,
"in_reply_to_status_id" : null,
"in_reply_to_status_id_str" : null,
"in_reply_to_user_id" : null,
"in_reply_to_user_id_str" : null,
"lang" : "en",
"place" : null,
"user" : { "contributors_enabled" : false,
    "follow_request_sent" : null,
    "followers_count" : 1218,
    "id" :33333,
    "id_str" : "609465835",
    "is_translation_enabled" : false,
    "is_translator" : false,
    "lang" : "en",
    "listed_count" : 3,
    "location" : "N",
    "verified" : false
  }
}]

and the output is

Json Output

I am trying to work with Newtonsoft Json deserialize, but failed.

Following is the code:

        dynamic dynObj = JsonConvert.DeserializeObject(twitAuthResponse);
        foreach (var data in dynObj.user.data)
        {
            //Console.WriteLine("{0}", data.name);
            foreach (var fql in data.user)
            {
                foreach (JProperty keyValue in fql)
                {
                    Console.WriteLine("\t{0} : {1}", keyValue.Name, keyValue.Value);
                }
            }
        }

The above code returns error " 'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'user' ".

Could someone please help me out? Thanks in advance!


Solution

  • Finally solved,

    dynamic stuff = JsonConvert.DeserializeObject(twitAuthResponse);
    
    foreach (JObject item in stuff)
    {
        foreach (JProperty trend in item["user"])
        {
            if (trend.Name == "name")
            {
                 // GET NAME
            }
            else if (trend.Name == "followers_count")
            {
                 // GET COUNT
            }
            else if (trend.Name == "profile_image_url")
            {
                 // GET PROFILE URL
            }
        }
    }