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.

CDspace
  • 2,639
  • 18
  • 30
  • 36
ProgSky
  • 2,530
  • 8
  • 39
  • 65

1 Answers1

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();
                }
            }
ProgSky
  • 2,530
  • 8
  • 39
  • 65
  • 1
    You could deserialize directly to a `List` without the loop; the important thing is to stream directly without an intermediate `string`. See [Performance Tips](http://www.newtonsoft.com/json/help/html/Performance.htm): *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.* – dbc Apr 04 '17 at 01:16
  • 1
    Thanks! you are right. I was able to Deserialize as var listHolding = serializer.Deserialize>(reader); – ProgSky Apr 04 '17 at 15:05