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": "[email protected]",
  "firstName": "xxx",
  "formattedName": "xxxx xxxx",
  "id": "xxxxxx",
  "lastName": "xxxxxx",

}
1
  • 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. Commented Dec 27, 2016 at 3:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

All answers given here works perfectly. Do you suggest that @Mohit 's answer is the best way to go ahead ?
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);

1 Comment

Thank you for your reply. Your solution works as well.
1
result={
         "emailAddress": "[email protected]",
         "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;
  ...
}

1 Comment

Thank you for your reply. Your solution works as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.