问题描述
我,如果我的JSON文件看起来像这样
I have an issue where if my json file looks like this
{数字:45387,词:空水桶}
{ "Numbers": "45387", "Words": "space buckets"}
我可以读它只是罚款,但是如果它看起来像这样:
I can read it just fine, however if it looks like this:
{主:{号:45387,词:太空桶},结果
东西:{数字:12345,词:Kransky}}
{ "Main" :{ "Numbers": "45387", "Words": "space buckets"},
"Something" :{"Numbers": "12345", "Words": "Kransky"} }
我没有得到任何信息反馈。我不知道如何和主要的东西之间切换!
使用此代码加载与此嵌套信息的JSON,
I get no information back. I have no idea how to switch between Main and Something!Loading a JSON with this 'nested' information using this code,
var ser = new DataContractJsonSerializer(typeof(myInfo));
var info = (myInfo)ser.ReadObject(e.Result);
//使用来保存我的信息类是
// The class being using to hold my information
[DataContract]
public class myInfo
{
[DataMember(Name="Numbers")]
public int number
{ get; set; }
[DataMember(Name="Words")]
public string words
{ get; set; }
}
导致该类回来空。结果
我试着添加组名DataContract如。 [DataContract,名称=主],但这还是引起类值是空的。结果
我也尝试添加主到串行overloader如。 VAR SER =新DataContractJsonSerializer(typeof运算(MyInfo的),主);结果
这将导致一个错误:期待元素主从命名空间''..遇到元素名为'根',命名空间''。结果
我宁愿只使用JSON提供读者。我已经看着json.NET,但已经找到了文档,重写作JSON和稀疏的有关阅读的信息。 !
当然,我失去了一些东西在这里简单
Causes the class to come back empty.
I've tried adding the group name to DataContract eg. [DataContract, Name="Main"] but this still causes the classes values to be empty.
I've also tried adding "main" to the serializer overloader eg. var ser = new DataContractJsonSerializer(typeof(myInfo), "Main");
This causes an error: Expecting element 'Main' from namespace ''.. Encountered 'Element' with name 'root', namespace ''.
I'd prefer to just use the supplied json reader. I have looked into json.NET but have found the documentation to be heavy on writing json and sparse with information about reading.Surely I'm missing something simple here!
推荐答案
您可以添加一个包装类:
You could add a wrapper class:
[DataContract]
public class Wrapper
{
[DataMember]
public myInfo Main { get; set; }
[DataMember]
public myInfo Something { get; set; }
}
现在,你可以反序列化JSON回到这个包装类,并使用双属性来访问的值。
Now you could deserialize the JSON back to this wrapper class and use the two properties to access the values.
这篇关于我将如何读入一个“嵌套”的Json与'DataContractJsonSerializer“在C#.NET(WIN7电话)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!