Reputation: 1553
I have json that need to get the value from the json in c# language. Usually i got the data from json by creating class and it is work. But now when i use the same way to get value from JSON there have an error appear. That error is :
'string' does not contain a definition for 'data'
I am not sure where is my mistake. This is how I get the value from JSON and supposedly it is work. The Class:
public class InstaComments
{
public Data[] data { get; set; }
public class Data
{
public string created_time { get; set; }
public string text { get; set; }
public string full_name { get; set; }
}
}
And the error appear at this line of code:
List dyn = JsonConvert.DeserializeObject>(resultComments.data.ToString());//error on here
This is my Json code:
"{\"meta\":{\"code\":200},\"data\":{\"created_time\":\"1406056452\",\"text\":\"Cool!\",\"from\":{\"username\":\"s19xx_\",\"profile_picture\":\"http:\\/\\/photos-d.ak.instagram.com\\/hphotos-ak-xaf1\\/10499142_1462797113973499_926138390_a.jpg\",\"id\":\"445755252\",\"full_name\":\"\\ud83d\\udc97\"},\"id\":\"770379817908166709\"},{\"created_time\":\"1406423741\",\"text\":\"Nice pic!\",\"from\":{\"username\":\"zaffique\",\"profile_picture\":\"http:\\/\\/images.ak.instagram.com\\/profiles\\/profile_143827767_75sq_1378666562.jpg\",\"id\":\"143827767\",\"full_name\":\"Chaperone molecule\\u2122\"},\"id\":\"773460860856951112\"},{\"created_time\":\"1406466052\",\"text\":\"@zaffique yeahhh\",\"from\":{\"username\":\"anakshawal\",\"profile_picture\":\"http:\\/\\/photos-g.ak.instagram.com\\/hphotos-ak-xaf1\\/10584712_1532798830282582_253819990_a.jpg\",\"id\":\"22545759\",\"full_name\":\"\"},\"id\":\"773815792877429301\"},{\"created_time\":\"1406509023\",\"text\":\"Good shot!\",\"from\":{\"username\":\"c_uniqueroom\",\"profile_picture\":\"http:\\/\\/photos-e.ak.instagram.com\\/hphotos-ak-xfp1\\/10547229_328439730654500_459648312_a.jpg\",\"id\":\"456088491\",\"full_name\":\"C_uniqueroom\"},\"id\":\"774176262612312566\"},{\"created_time\":\"1408277876\",\"text\":\"Lol\",\"from\":{\"username\":\"adieruddinwanahmad\",\"profile_picture\":\"http:\\/\\/photos-e.ak.instagram.com\\/hphotos-ak-xpa1\\/10471808_1459922647581324_961927518_a.jpg\",\"id\":\"355537664\",\"full_name\":\"wan adieruddin wan ahmad\"},\"id\":\"789014471007527397\"},{\"created_time\":\"1408279167\",\"text\":\"@adieruddinwanahmad he is almost there\",\"from\":{\"username\":\"anakshawal\",\"profile_picture\":\"http:\\/\\/photos-g.ak.instagram.com\\/hphotos-ak-xaf1\\/10584712_1532798830282582_253819990_a.jpg\",\"id\":\"22545759\",\"full_name\":\"\"},\"id\":\"789025304592769150\"},{\"created_time\":\"1408310569\",\"text\":\"Please @anakshawal I can be distressed about this situation he really badly guy\",\"from\":{\"username\":\"adieruddinwanahmad\",\"profile_picture\":\"http:\\/\\/photos-e.ak.instagram.com\\/hphotos-ak-xpa1\\/10471808_1459922647581324_961927518_a.jpg\",\"id\":\"355537664\",\"full_name\":\"wan adieruddin wan ahmad\"},\"id\":\"789288719299108925\"}]}"
Please anybody tell me where is my mistake. Thanks in advance.
Upvotes: 0
Views: 353
Reputation: 458
It seems that your resultComments
variable is string. So it does not have any properties like data or meta. You have to deserialize this string first and then get data from the deserialized object:
dynamic comments = JsonConvert.DeserializeObject(resultComments);
dynamic data = comments.data; // Your data is here
Upvotes: 1
Reputation: 835
With Nate Kerkhofs' answer, one can easily see that your Json data doesn't match your class.
Using this link, you can see that it has to look more like this :
public class From
{
public string username { get; set; }
public string profile_picture { get; set; }
public string id { get; set; }
public string full_name { get; set; }
}
public class Datum
{
public string created_time { get; set; }
public string text { get; set; }
public From from { get; set; }
public string id { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
Upvotes: 0
Reputation: 3565
It appears like your json string does not match your object.
{
"data": [
{
"created_time": "1406056452",
"text": "Cool!",
"from": {
"username": "s19xx_",
"profile_picture": "http://photos-d.ak.instagram.com/hphotos-ak-xaf1/10499142_1462797113973499_926138390_a.jpg",
"id": "445755252",
"full_name": "\\ud83d\\udc97"
},
"id": "770379817908166709"
}
]
}
This is how your JSON looks like with proper indentation and escaping. This does not at all match the data object you gave above. More specifically, your data object is missing an id attribute, and you need to replace the full_name string with an implementation of the from object.
Upvotes: 0
Reputation: 2674
From the look of it, your JSON isn't JSON, it's a string that could be parsed to JSON.
To get it using the same data source, you'd need to de-serialise it twice. Once to get the string and another time to get the JSON from the string.
A better way to do it would be to get whatever is sending the JSON to not enclose it in a string. That may be as simple as just not calling .ToString
on your resultComments.data
, but it's more likely that the source of your data is accidently turning the JSON into a single string.
Upvotes: 1