简体   繁体   中英

extracting data from big JSON

We have a very big JSON, close to 1500 fields. We want to read most of the JSON fields (Most of the time I have to read whole JSON and get 80% of data). What could be the most performance optimized way ? should we use JObject.Parse and then use JsonPath or should we use JsonConvert.DeserializeObject and then use linq queries or are there any other better ways

Since you mentioned it's a big JSON, I will suggest you to use a stram reader so you don't need to load the whole JSON into memory

    using (var reader = new JsonTextReader(new StreamReader(stream)))
    {
        while(!(reader.TokenType.Equals(JsonToken.EndObject) && reader.Depth == 0))
        {
            if (reader.TokenType.Equals(JsonToken.PropertyName))  
                 if (!DoSomething(reader))
                    break;
            if (!reader.Read())
                break;
        }
    }

In your DoingSomething method, you can for example, read value from one field

    private bool DoSomething(JsonTextReader reader)
    {
        if(reader.Path.Equals("FieldName_You_Are_Looking_For"))
        {
            string jsonText = JsonTextReaderToJsonText(reader);
            // Do something to read the value
        }
        // return true to continue reading, return false to stop reading
        return true;
    }

Hope it can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM