Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a json model. In this model i want to get the name of each value in this json using c#. If you look to the picture, you will see what i want. Can you help me please?

What I have tried:

I have a json model. In this model i want to get the name of each value in this json using c#. If you look to the picture, you will see what i want. Can you help me please?

dynamic model = JsonConvert.DeserializeObject<string>(noteText);
var n= model.locations[0].name; //It gives me The 'string' does not contain a 'locations' definition error
Posted
Updated 12-Nov-20 10:37am

C#
string noteText = "{locations : [{name: \"loc1\"}, {name:\"loc2\"}]}";

// you're converting to dynamic so use that in the angled brackets, not string
dynamic model = JsonConvert.DeserializeObject<dynamic>(noteText);
var n = model.locations[0].name;


If it still doesn't work then check your code matches the shape of your json objects.
 
Share this answer
 
v2
Comments
muharrem altun 13-Nov-20 0:41am    
I tried you soulution but i got the error in this line: var n=model.locations[0].name;
The error message is : string 'does not contain a' location 'definition.
My json begins like this:
{ "locations": [ { "name": "accounts/103890250566243670362/locations/17566485131419324180", "storeCode": "006841020I01", "locationName": "İstikbal - ATAK TICARET", "primaryPhone": "(0312) 814 10 22", "primaryCategory": { "displayName": "Mobilya mağazası", "categoryId": "gcid:furniture_store" }, "websiteUrl": "http://www.istikbal.com.tr/", "regularHours": { "periods": [ { "openDay": "SUNDAY", "openTime": "09:00", "closeDay": "SUNDAY", "closeTime": "21:00" }, { "openDay": "MONDAY", "openTime": "09:00", "closeDay": "MONDAY", "closeTime": "21:00" }, { "openDay": "TUESDAY", "openTime": "09:00", "closeDay": "TUESDAY", "closeTime": "21:00" }, { "openDay": "WEDNESDAY", "openTime": "09:00", "closeDay": "WEDNESDAY", "closeTime": "21:00" }, { "openDay": "THURSDAY", "openTime": "09:00", "closeDay": "THURSDAY", "closeTime": "21:00" }, { "openDay": "FRIDAY", "openTime": "09:00", "closeDay": "FRIDAY", "closeTime": "21:00" }, { "openDay": "SATURDAY", "openTime": "09:00", "closeDay": "SATURDAY", "closeTime": "21:00" } ] }, "locationKey": { "placeId": "ChIJux7UUUVwgkAR4AuSDLY30hY", .....
muharrem altun 13-Nov-20 0:50am    
Thank you. I found the solution with your approach.
You can also try using JObject and access the element via keys. For example:

C#
var data = JObject.Parse("Some Json String");
var elementValue = data["SomeProperty"];


Another approach is using JsonDocument. For example:

C#
var jsonDocument = JsonDocument.Parse("Some Json String");

var elementValue = jsonDocument.RootElement.GetProperty("SomeProperty").GetString();

var jsonElement = jsonDocument.RootElement.TryGetProperty("SomeProperty", out var targetElement);

if (!jsonElement)
{
  // Element not found
}
 
Share this answer
 
v2
Comments
muharrem altun 13-Nov-20 0:55am    
thank you for your help. I found the solution:
dynamic model = JsonConvert.DeserializeObject<dynamic>(response1.Content.ToString());
var n = model.locations[0].name;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900