0

I am facing an issue in parsing a JSON data which is retrieved from a javascript file (say xyz.js) The file size is about 10mb and only one JSON string is present in it.

We were using javascript directly to parse this file, I want to know whether we have any C# classes to parse this file or not?

example of the JSON string is

var JSONString = {
    Level1:{
        DateTime:{
              date:'Wed Sep 14 14:19:32 CDT 2011'
        },
        information:{
              url:'http:\\www.google.com'
        },
        RepetitiveLevel:[
            {
                ids:{
                    courses:[
                        "BE",
                        "MS"
                    ]
                },
                SubRepetitiveLevels:{
                    DetailedLevel:[
                        {
                            name:"Trial 1",
                            type:"blah",
                            latest:"no",
                            version:"1",
                            description:"This is a test 1.",
                            recommendation:"blah",
                            prerequisite:"<strong>WARNING!</strong> xyz",
                            releasedate:"2012-06-18T15:38:55.79",
                            support:{
                                degree:[
                                    "MSC",
                                    "BSC",
                                    "HSE"
                                ]
                            },
                            OperatingSystem:{
                                os:["Win 2008 x64"
                                ]
                            },
                            previousversions:{
                                version:[
                                ]
                            },
                            allversions:{
                                version:[
                                    "1",
                                    "2",
                                    "3"
                                ]
                            },
                            ftppath:"ftp://",
                            files:{
                                file:["note1.txt"
                                ]
                            },
                            filesizes:{
                                filesize:["5MB"
                                ]
                            },
                            checkSum:{
                                md5:["abc"
                                ]
                            },
                            note:"<P><STRONG>NOTE: </STRONG> NOTE 1 </LI></UL></OL>",
                            fix:"<P style=\"MARGIN-TOP: 4pt; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0pt; COLOR: #000000; FONT-FAMILY: Arial\"> xyz <STRONG></P></LI></UL>"                           

                        },
                        {
                            name:"Trial 2",
                            type:"blah",
                            latest:"yes",
                            version:"2",
                            description:"This is a test 2.",
                            recommendation:"blah",
                            prerequisite:"<strong>WARNING!</strong> xyz",
                            releasedate:"2012-06-18T15:38:55.79",
                            support:{
                                degree:[
                                    "MCA",
                                    "BCA",
                                    "BE"
                                ]
                            },
                            OperatingSystem:{
                                os:["Win XP"
                                ]
                            },
                            previousversions:{
                                version:[
                                ]
                            },
                            allversions:{
                                version:[
                                    "4",
                                    "5",
                                    "6"
                                ]
                            },
                            ftppath:"ftp://",
                            files:{
                                file:["Note2.txt"
                                ]
                            },
                            filesizes:{
                                filesize:["2MB"
                                ]
                            },
                            checkSum:{
                                md5:["abc"
                                ]
                            },
                            note:"<P><STRONG>NOTE: </STRONG> NOTE 2 </LI></UL></OL>",
                            fix:"<P style=\"MARGIN-TOP: 4pt; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0pt; COLOR: #000000; FONT-FAMILY: Arial\"> ahahah <STRONG></P></LI></UL>"                           

                        }


                    ]
                }
            }
        ]
    }
}



The tag named "RepetitiveLevel" will be repeating more than 10 times 
Under this tag, there will be repetitions of "SubRepetitiveLevels", which in turn contains more than one entry for "DetailedLevel". 
This kind of JSON string fails for Newtonsoft.Json.JsonConvert.DeserializeObject 

   I know this is a little bit confusing, but we are not finding any other option. 

Any help will suffice. Thanks in advance.

Mparame
  • 37
  • 8
  • 1
    read **Marc Gravell** answer from this question http://stackoverflow.com/questions/401756/parsing-json-using-json-net – Buzz Sep 10 '12 at 10:09

1 Answers1

1

Try this library. We used it on one ASP.NET project and it worked fine.

This program writes then reads 100mb file with serialization/deserialization.

class Program
{
    static void Main(string[] args)
    {
        var lst = new List<string>();
        for (var i = 0; i < 1024 * 1024 * 10; i++)
        {
            lst.Add(i.ToString());
            if(i%(1024 * 1024)==0)Console.WriteLine("+1m");
        }
        var wrt = Newtonsoft.Json.JsonConvert.SerializeObject(lst);
        lst = null;
        File.WriteAllText(@"F:\1.txt",wrt);
        Console.WriteLine("written");
        wrt = "";
        GC.Collect();
        wrt=File.ReadAllText(@"F:\1.txt");
        lst=Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(wrt);
        Console.WriteLine("read");
        Console.WriteLine(lst.Count.ToString());
    }
}
FLCL
  • 2,445
  • 2
  • 24
  • 45
  • The problem I have in hand is that the size of JSON string read from the file will be very huge (it is 10 MB, and it is a single JSON sting value ). Normal StreaMReader objects cant handle this much of buffer. Any Suggestions ???? :) – Mparame Sep 10 '12 at 12:00
  • I tried your code though it worked for normal JSON string, it failed for the JSON string given above. any idea where i need to make the changes ? Thanks in advance – Mparame Sep 11 '12 at 11:12
  • You have error in your js-code here: catagory:[... but where "]"? – FLCL Sep 11 '12 at 18:24
  • Sorry my bad, i missed some stuffs while editing the whole file, I have updated the file contents now, this doesnt have any flaws (i think so) but this format also fails. – Mparame Sep 13 '12 at 06:36
  • I copy-pasted your json, removed "var JSONString =" and it was deserialized with no error. – FLCL Sep 13 '12 at 10:34
  • @ Aleksey I removed that part as you suggested ("var JSONString ="), but i got the following error. I tried to deserialize to an "Object" type, that also throws up same exception. Do I need to specify any class having this particular structure in order to make this work ? – Mparame Sep 14 '12 at 04:52
  • Error i got was :- Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. – Mparame Sep 14 '12 at 04:52
  • List lst = new List(); var wrt = File.ReadAllText("Example.txt"); lst = Newtonsoft.Json.JsonConvert.DeserializeObject>(wrt); – Mparame Sep 15 '12 at 04:05
  • I think List is incorrect for deserialization of your json example. You may try to deserialize object without <> template cast and access fields via in build JObject class. – FLCL Sep 16 '12 at 15:56
  • @ Aleksey Sorry, I didnt get you, – Mparame Sep 18 '12 at 05:44
  • JsonConvert.DeserializeObject>(wrt) will deserialize an array, but you have object of another structure so you should create c# class with equal to json in file structure and cast YourEqualToJsonClass result= JsonConvert.DeserializeObject(wrt) – FLCL Sep 18 '12 at 09:29
  • Thanks a lot Aleksey , I was thinking the same, I had asked you once about this, but I think I didnt make it clear at that time, sorry for that, Your suggestions and examples helped me a lot to understand this deserialization. – Mparame Sep 22 '12 at 04:49