我将如何读入“嵌套”?带有“DataContractJsonSerializer”的 Json 文件在C# .NET(win7手机)中?

发布于 2024-10-08 17:12:26 字数 1010 浏览 0 评论 0原文

我有一个问题,如果我的 json 文件看起来像这样

{ "Numbers": "45387", "Words": "space buckets"}

我可以很好地阅读它,但是如果它看起来像这样:

{ "Main" :{ “数字”:“45387”,“单词”:“空间桶”},
"Something" :{"Numbers": "12345", "Words": "Kransky"} }

我没有收到任何信息。我不知道如何在 Main 和 Something 之间切换! 使用此代码加载包含此“嵌套”信息的 JSON,

var ser = new DataContractJsonSerializer(typeof(myInfo));

var info = (myInfo)ser.ReadObject(e.Result); 

// 用于保存我的信息的类

[DataContract] 
public class myInfo 
{ 
    [DataMember(Name="Numbers")] 
    public int number 
    { get; set; } 

    [DataMember(Name="Words")] 
    public string words 
    { get; set; } 
} 

导致该类返回空。
我尝试将组名称添加到 DataContract 例如。 [DataContract, Name="Main"] 但这仍然会导致类值为空。
我还尝试将“main”添加到序列化器重载器中,例如。 var ser = new DataContractJsonSerializer(typeof(myInfo), "Main");
这会导致错误:期望来自命名空间“”的元素“Main”..遇到名称为“root”、命名空间“”的“元素”。
我更愿意只使用提供的 json 阅读器。我研究过 json.NET,但发现文档中关于编写 json 的内容较多,而有关阅读的信息却很少。 我肯定在这里错过了一些简单的东西!

I have an issue where if my json file looks like this

{ "Numbers": "45387", "Words": "space buckets"}

I can read it just fine, however if it looks like this:

{ "Main" :{ "Numbers": "45387", "Words": "space buckets"},
"Something" :{"Numbers": "12345", "Words": "Kransky"} }

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; } 
} 

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2024-10-15 17:12:27

您可以添加一个包装类:

[DataContract]
public class Wrapper
{
    [DataMember]
    public myInfo Main { get; set; }

    [DataMember]
    public myInfo Something { get; set; }
}

现在您可以将 JSON 反序列化回该包装类,并使用这两个属性来访问值。

You could add a wrapper class:

[DataContract]
public class Wrapper
{
    [DataMember]
    public myInfo Main { get; set; }

    [DataMember]
    public myInfo Something { get; set; }
}

Now you could deserialize the JSON back to this wrapper class and use the two properties to access the values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文