Reputation: 17
I'm trying to parse a big JSON array into a c# object. Now I know of the normal way where you take a class with matching keys to the JSON object and then assign each value of the JSON object to the matching key in the c# class. but this would be horribly inefficient for my purpose because im dealing with very large JSON objects here is an example:
[{
"storeId": "331",
"storeName": "Bergen, Lagunen",
"status": "Open",
"address": {
"street": "Laguneveien 1",
"postalCode": "5239",
"city": "Rådal",
"gpsCoord": "60.297116;5.331159",
"globalLocationNumber": "7080003251008",
"organisationNumber": "888039902"
},
"telephone": "22 01 50 00",
"email": "[email protected]",
"category": "6",
"openingHours": {
"regularHours": [{
"dayOfTheWeek": "Monday",
"openingTime": "10:00",
"closingTime": "18:00",
"closed": false
}, {
"dayOfTheWeek": "Tuesday",
"openingTime": "10:00",
"closingTime": "18:00",
"closed": false
}, {
"dayOfTheWeek": "Wednesday",
"openingTime": "10:00",
"closingTime": "18:00",
"closed": false
}, {
"dayOfTheWeek": "Thursday",
"openingTime": "10:00",
"closingTime": "18:00",
"closed": false
}, {
"dayOfTheWeek": "Friday",
"openingTime": "10:00",
"closingTime": "18:00",
"closed": false
}, {
"dayOfTheWeek": "Saturday",
"openingTime": "10:00",
"closingTime": "15:00",
"closed": false
}, {
"dayOfTheWeek": "Sunday",
"openingTime": "",
"closingTime": "",
"closed": true
}],
"exceptionHours": [{
"date": "2020-05-21",
"openingTime": "",
"closingTime": "",
"message": "Stengt Kristi himmelfartsdag"
}]
},
"lastChanged": {
"date": "2020-05-04",
"time": "00:02:04"
}
}]
is there any way to dynamically make this into a c# object as you can in javascript using JSON.parse?
Upvotes: 0
Views: 720
Reputation: 4177
You say you have big json. I will assume that you say that because the json have many properties you don't need. If you do need all properties, I would just define a class containing all the properties.
That said. I think you have two real options if we'd go for Newtonsoft.Json.
public class Pruned
{
public string StoreId { get; set; }
}
var prunedList = JsonConvert.DeserializeObject<List<Pruned>>(myJsonString);
var jArray = JArray.Parse(myJsonString);
var firstStoreId = jArray[0]["storeId"];
Upvotes: 1