0

I am relatively new to Json.net. I have to parse a JSON which I am getting as an URL.

My Code Looks:

var url = "some json url";
using (WebClient wc = new WebClient())
{
    wc.UseDefaultCredentials = true;
    JArray arr = JArray.Parse(wc.DownloadString(url));
    var holdingRecords = arr.ToObject<List<HoldingData>>();
} 

This work fine with ConsoleApp. As soon as I put this in my ASP.net, wc.DownloadString(url) return OutOfMemory error.

HoldingData is a class with a bunch of properties. JSON is array of structure where each structure is a property of a class.

Any clue as of how can I resolve this. My JSON is huge, and I am looking for the best solution.

6
  • 4
    My Json is huge define huge Commented Apr 3, 2017 at 20:34
  • Any reason why you're not using HttpClient instead? Commented Apr 3, 2017 at 20:49
  • Thanks David. I am trying to use HttpWebRequest to see if that helps. Commented Apr 3, 2017 at 20:59
  • it would be great if you can provide your JSON response. Commented Apr 3, 2017 at 21:07
  • Sameer, I can not provide the json but this is how it looks: Commented Apr 3, 2017 at 21:31

1 Answer 1

1

I used something like below and it does what I want. But I was wondering if there is a better way

using (WebClient client = new WebClient())
            {
                client.UseDefaultCredentials = true;
                using (Stream stream = client.OpenRead(url))
                using (StreamReader streamReader = new StreamReader(stream))
                using (JsonTextReader reader = new JsonTextReader(streamReader))
                {
                    reader.SupportMultipleContent = true;
                    List<HoldingData> hd = new List<HoldingData>();
                    var serializer = new JsonSerializer();
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.StartObject)
                        {
                            HoldingData c = serializer.Deserialize<HoldingData>(reader);
                            hd.Add(c);
                        }
                    }

                    Console.ReadLine();
                }
            }
Sign up to request clarification or add additional context in comments.

2 Comments

You could deserialize directly to a List<HoldingData> without the loop; the important thing is to stream directly without an intermediate string. See Performance Tips: To minimize memory usage and the number of objects allocated, Json.NET supports serializing and deserializing directly to a stream. Reading or writing JSON a piece at a time, instead of having the entire JSON string loaded into memory, is especially important when working with JSON documents ... to avoid the JSON string ending up in the large object heap.
Thanks! you are right. I was able to Deserialize as var listHolding = serializer.Deserialize<List<HoldingData>>(reader);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.