0

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?

2
  • Check out Newtonsoft.Json and in particular the JArray.Parse method. Commented May 6, 2020 at 20:25
  • I don't see why you shouldn't create a class matching your object. It doesn't seem exceptionally large to me Commented May 6, 2020 at 20:26

1 Answer 1

1

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.

1. Define a class with only the properties you need

public class Pruned
{
    public string StoreId { get; set; }
}

var prunedList = JsonConvert.DeserializeObject<List<Pruned>>(myJsonString);

2. Deserialize into a JArray and treat it as a dynamic object.

var jArray = JArray.Parse(myJsonString);
var firstStoreId = jArray[0]["storeId"];
Sign up to request clarification or add additional context in comments.

Comments

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.