0

I am developing facebook app in which i am fetching user's friend detail in as

dynamic result = client.Get("me/friends"); //it gives friend's data for id, name

it gives data in

{
  "data": [
    {
      "name": "Steven", 
      "id": "57564897"
    }, 
    {
      "name": "Andy", 
      "id": "8487581"
    }
}

Now i would like to parse this data and store it. so that i can use it my way.

I was trying to parse it using JSON.NET and show the data in view as

var model = JsonConvert.DeserializeObject<FriendDetail>(result.data);

in the class :

public class FriendDetail
    {
        public string id { get; set; }
        public string name { get; set; }

        public FriendDetail(string i, string n)
        {
            id = i;
            name = n;
        }
    }

Now so that i can pass the view as "return View(model)"

But its giving me error: The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<FBApp.Models.FBFriendDetail>(string)' has some invalid arguments

Why this error is occurring ?

Please help me to parse this json data.

Also is there any better way to parse and Store json data and also then show in view ?

Please help

1 Answer 1

4

You are trying to deserialize a list of FriendDetail objects into a single FriendDetail object. Try the following:

var jObject = JObject.Parse(result.ToString());
var model = JsonConvert.DeserializeObject<List<FriendDetail>>(jObject["data"].ToString());

EDIT

This is how I tested it:

var result = 
    @"{
        ""data"": [
        {
            ""name"": ""Steven"", 
            ""id"": ""57564897""
        }, 
        {
            ""name"": ""Andy"", 
            ""id"": ""8487581""
        }]
    }";

var jObject = JObject.Parse(result.ToString());
var model = JsonConvert.DeserializeObject<List<FriendDetail>>(jObject["data"].ToString());
Sign up to request clarification or add additional context in comments.

3 Comments

giving error The best overloaded method match for 'Newtonsoft.Json.Linq.JObject.Parse(string)' has some invalid arguments
I have just pasted the code that I used to test it. Does that not work on your machine? Note that I had to add a closing brace ] to the end of your JSON array (it was missing in your example). Could that be the issue?
I also see that result is dynamic so I'm not sure what type it is. Try calling .ToString() or it before passing it to JObject. I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.