简体   繁体   中英

Deserialize Big JSON Array and extracting data from it

Currently i'am working on a project that use Azure Media Indexing services (extract text from video etc..). After indexing i get the response result as json. With this website ( http://json2csharp.com/ ) i have created the view model for my project. I want to extract certain data from this json. Now the problem is when i try to parse the json, the system is stuck on the deserializing line.

Basically i tried to deserialize like below,

IList<IndexedVideoReponseVM> modelObj = JsonConvert.DeserializeObject<IList<IndexedVideoReponseVM>>(responseData);

I have tried almost all the code snippets available in stackoverflow like javascript serializer, JSON.net etc... I think maybe its because of the size of the json. I have uploaded the sample json to here ( http://myjson.com/1g74kx ) and here is my VM ( http://textuploader.com/d6218 ). Could some one please analyze this and let me know how to resolve this?

Thanks you.

The JSON at your link is not an array, it is a single element. On top of that you wrapped all generated classes including class RootObject in your IndexedVideoReponseVM class, but that is not the right approach.

Please remove the wrapping class IndexedVideoReponseVM (keep all that is inside), and then deserialize the JSON like this: JsonConvert.DeserializeObject<RootObject>(responseData) .

Of course once that works you can give RootObject a more fitting name (json2sharp could not do that because it has no information about the outer object).

I think it has do to something with your JSON being double quotationed. I had a similar issue a while back, i tried deserializing it after processed through escaper and it worked for me :) You should escape the received JSON first or replace a double quotes if u can.

You should deserialize like this:

var obj = JsonConvert.DeserializeObject<IndexedVideoReponseVM.RootObject>(str);

this code executes at 435 ms (less then 1 second)

full code example:

using System;
using System.Diagnostics;
using System.Threading;
using Newtonsoft.Json;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {


            var str = @"{
  ""accountId"": ""496c-aed1-ab750d882fa5"",
  ""id"": ""acd8121234"",

//i've cut most part of text couse of stackoverflow.com limitation on 30000 symbols. Code was tested with full json

  ""social"": {
    ""likedByUser"": false,
    ""likes"": 0,
    ""views"": 0
  }
}";

            var sw = Stopwatch.StartNew();
            var obj = JsonConvert.DeserializeObject<IndexedVideoReponseVM.RootObject>(str);
            sw.Stop();
            Console.WriteLine($"Deserialized at {sw.ElapsedMilliseconds} ms ({sw.ElapsedTicks} tiks)");
        }
    }
}

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