【问题标题】:JSON Parsing in c#. The Json string is in a javascript fileC# 中的 JSON 解析。 Json 字符串位于 javascript 文件中
【发布时间】:2012-09-10 10:05:11
【问题描述】:

我在解析从 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. 

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

【问题讨论】:

标签: json c#-4.0


【解决方案1】:

试试this 库。我们在一个 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());
    }
}

【讨论】:

  • 我手头的问题是从文件中读取的 JSON 字符串的大小会非常大(它是 10 MB,它是单个 JSON 字符串值)。普通的 StreaMReader 对象无法处理这么多的缓冲区。有什么建议么 ???? :)
  • 我尝试了您的代码,尽管它适用于普通的 JSON 字符串,但对于上面给出的 JSON 字符串却失败了。知道我需要在哪里进行更改吗?提前致谢
  • 您的 js 代码中有错误:category:[... but where "]"?
  • 对不起,我在编辑整个文件时遗漏了一些东西,我现在已经更新了文件内容,这没有任何缺陷(我认为是这样)但这种格式也失败了。
  • 我复制粘贴了你的 json,删除了“var JSONString =”,它被反序列化,没有错误。
猜你喜欢
  • 2011-12-24
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多