C#解析没有名称的Json文件 [英] C# Parsing Json File with no name

查看:362
本文介绍了C#解析没有名称的Json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,Json文件的一部分是这样的:

So, part of the Json file is like this:

{
"number":"23",
"name":{
    "":"LJames"   <----------- look at this line
},
"Gender":"Male",
...

我正在尝试解析它,并且只要有name字段(名称与值不匹配),DataContractJsonSerializer都将无法读取它.

I am trying to parse this, and as long as the name field is there (without a name match with the value), the DataContractJsonSerializer will fail to read it.

任何人都有使用这种Json文件的经验,请分享一些想法,谢谢.

Anyone has experience with this kind of Json file please share some idea, thank you.

推荐答案

您可以尝试使用 Json.NET 对其进行解析.它通常比DataContractJsonSerializer更好,并且具有更好的性能.我不确定这是否可以解决您的问题.

You could try using Json.NET to parse it. It generally works a lot better than DataContractJsonSerializer and has better performance. I'm not sure if that would solve your problem though.

如果考虑一下,结果对象在C#中会是什么样?从这样的JSON字符串中...

If you think about it, what would the resulting object look like in C#? From a JSON string like this...

{
     "name" : { "first" : "James" }
}

......我希望映射到具有名称"属性的C#对象,并且该名称"属性将引用具有第一"属性(将是一个字符串,其值为詹姆斯").

...I would expect to map to a C# object with a "name" property, and that "name" property would reference an object with a "first" property (which would be a string, with a value of "James").

因此,如果删除键值"first",那么解析器将如何知道如何映射(或命名)该属性? C#中没有诸如无名属性之类的东西.

So if you remove the key value "first", how will the parser know how to map (or how to name) the property? There's no such thing as a nameless property in C#.

我建议重新格式化您的Json文件(如果可能的话),使其看起来像这样:

I would suggest reformatting your Json file (if possible) to look like so:

{
    "number":"23",
    "name": "LJames",
    "Gender":"Male",
    ...

这篇关于C#解析没有名称的Json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆