0

I am trying to retrieve Value's using Key's in the JSON returned.

I tried the following but, none worked.

1.)

string email= json.emailAddress;

2.)

string email= json["emailAddress"].ToString();

Complete Code

 var api= new Uri("https://api.linkedin.com/v1/people/~:(picture-url)?format=json");
 using (var webClient = new WebClient())
 {
       webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);

       webClient.Headers.Add("x-li-format", "json");

       dynamic  json = webClient.DownloadString(api);
  }

JSON returned

{
  "emailAddress": "xxxx@xx.com",
  "firstName": "xxx",
  "formattedName": "xxxx xxxx",
  "id": "xxxxxx",
  "lastName": "xxxxxx",

}
Illep
  • 16,375
  • 46
  • 171
  • 302
  • There are times when `dynamic` is good to use, but when you know the return type of a method you should declare the variable of that type, i.e. `string json = webClient.DownloadString(api);`. Using `dynamic` doesn't give your variable magical properties - if it's a `string` it's still a `string` with `dynamic`. – Enigmativity Dec 27 '16 at 03:35

3 Answers3

4

To answer your question using your approach, the simplest way ( without using JSON.Net ) is to use the JavaScriptSerializer

// have this at the top with your using statements
using System.Web.Script.Serialization;

and in your code, use JavaScriptSerializer as shown below.

var api= new Uri("https://api.linkedin.com/v1/people/~:(picture-url)?format=json");
 using (var webClient = new WebClient())
 {
     webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);    
     webClient.Headers.Add("x-li-format", "json");
    string json = webClient.DownloadString(api);

    JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    dynamic data = serializer.Deserialize<object[]>(json);
    string emailAddress = data.emailAddress;
  }

The better way would be to create strong-typed POCOs for your return JSON data using something like http://json2csharp.com/ and then deserializing using JSON.Net Library.

Shiva
  • 20,575
  • 14
  • 82
  • 112
  • 1
    All answers given here works perfectly. Do you suggest that @Mohit 's answer is the best way to go ahead ? – Illep Dec 27 '16 at 03:42
3

You can install Newtonsoft Json.Net for retrieving value from a JSON string. You can simply create class like

public class Rootobject
{
    public string emailAddress { get; set; }
    public string firstName { get; set; }
    public string formattedName { get; set; }
    public string id { get; set; }
    public string lastName { get; set; }
}

and then Deserialize it with simple one line code.

var ser = JsonConvert.DeserializeObject<Rootobject>(YourJsonString);
Console.WriteLine(ser.emailAddress);
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1
result={
         "emailAddress": "xxxx@xx.com",
         "firstName": "xxx",
         "formattedName": "xxxx xxxx",
         "id": "xxxxxx",
         "lastName": "xxxxxx"
        }
 var ser = new JavaScriptSerializer();
 var people = ser.Deserialize<object[]>(result);
foreach(var obj in people)
{
  Email = obj.emailAddress;
  ...
}
Tran Anh Hien
  • 687
  • 8
  • 11