0

我在解析从 javascript 文件(比如 xyz.js)中检索到的 JSON 数据时遇到问题。文件大小约为 10mb,其中只有一个 JSON 字符串。

我们直接使用javascript来解析这个文件,我想知道我们是否有任何C#类来解析这个文件?

JSON 字符串的示例是

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. 

任何帮助就足够了。提前致谢。

4

1 回答 1

1

试试这个库。我们在一个 ASP.NET 项目中使用它,它运行良好。

该程序通过序列化/反序列化写入然后读取 100mb 文件。

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());
    }
}
于 2012-09-10T10:09:26.993 回答