Json.NET (Newtonsoft.Json) - 两个同名的“属性"? [英] Json.NET (Newtonsoft.Json) - Two 'properties' with same name?

查看:22
本文介绍了Json.NET (Newtonsoft.Json) - 两个同名的“属性"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 为 .NET Framework 3.5 编码.

我正在尝试将一些 Json 解析为 JObject.

Json 如下:

<代码>{TBox":{"名称": "小盒子",长度":1,宽度":1,"高度": 2 },TBox":{"Name": "MedBox",长度":5,宽度":10,高度":10 },TBox":{"Name": "LargeBox",长度":20,宽度":20,高度":10 }}

当我尝试将此 Json 解析为 JObject 时,JObject 只知道 LargeBox.SmallBox 和 MedBox 的信息丢失.显然这是因为它将TBox"解释为一个属性,并且该属性正在被覆盖.

我从用 Delphi 编码的服务接收此 Json.我正在尝试为该服务创建一个 C# 代理.在 Delphi 方面,TBox"被理解为被返回对象的类型.然后将内部属性(名称"、长度"、宽度"、高度")理解为常规属性.

我可以序列化和反序列化具有名称、长度、宽度和高度属性的自定义TBox"对象.没关系.

我想要做的是逐步遍历所有 TBox 部分,以便提取以下三个 Json 字符串.

首先:

<代码>{"名称": "小盒子",长度":1,宽度":1,高度":2 }

第二:

<代码>{名称":医疗箱"长度":5,宽度":10,高度":10 }

第三:

<代码>{名称":大盒子"长度":20,宽度":20,高度":10 }

一旦我有了这些字符串,我就可以序列化和反序列化到我心中的内容.

我发现 Newtonsoft.Json 非常好.如果可以避免的话,我真的不想去搞乱其他框架.

任何帮助将不胜感激.

对于可以对服务器进行的更改,我的意见非常有限.

解决方案

using Newtonsoft.Json;使用 Newtonsoft.Json.Linq;JsonTextReader jsonReader = new JsonTextReader(reader);jsonReader.Read();while(jsonReader.Read()){如果(jsonReader.TokenType == JsonToken.StartObject){JObject tbox = JObject.Load(jsonReader);}}

但是,请注意 RFC 说,对象内的名称应该是唯一",所以如果可以,建议更改格式.

这是一个没有重复键的替代设计:

<预><代码>[{TBox":{宽度":1,长度":1,"名称": "小盒子",高度":2}},{TBox":{宽度":10,长度":5,"Name": "MedBox",高度":10}},{TBox":{宽度":20,长度":20,"Name": "LargeBox",高度":10}}]

I'm coding in C# for the .NET Framework 3.5.

I am trying to parse some Json to a JObject.

The Json is as follows:

{
    "TBox": {
        "Name": "SmallBox",
        "Length": 1,
        "Width": 1,
        "Height": 2 },
    "TBox": {
        "Name": "MedBox",
        "Length": 5,
        "Width": 10,
        "Height": 10 },
    "TBox": {
        "Name": "LargeBox",
        "Length": 20,
        "Width": 20,
        "Height": 10 }
}

When I try to parse this Json to a JObject, the JObject only knows about LargeBox. The information for SmallBox and MedBox is lost. Obviously this is because it is interpreting "TBox" as a property, and that property is being overwritten.

I am receiving this Json from a service that's coded in Delphi. I'm trying to create a C# proxy for that service. On the Delphi-side of things, the "TBox" is understood as the type of the object being returned. The inner properties ("Name", "Length", "Width", "Height") are then understood as regular properties.

I can serialize and deserialize a custom 'TBox' object that has Name, Length, Width, and Height properties. That's fine.

What I want to do is step through all the TBox sections in such a way as to extract the following three Json strings.

First:

{
    "Name": "SmallBox",
    "Length": 1,
    "Width": 1,
    "Height": 2 }

Second:

{
    "Name": "MedBox"
    "Length": 5,
    "Width": 10,
    "Height": 10 }

Third:

{
    "Name": "LargeBox"
    "Length": 20,
    "Width": 20,
    "Height": 10 }

Once I have these strings, I can serialize and deserialize to my heart's content.

I'm finding Newtonsoft.Json to be very good. I really don't want to go messing about with other frameworks if I can avoid it.

Any help would be greatly appreciated.

I have very limited input as to changes that can be made to the server.

解决方案

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

JsonTextReader jsonReader = new JsonTextReader(reader);
jsonReader.Read();
while(jsonReader.Read())
{
    if(jsonReader.TokenType == JsonToken.StartObject)
    {
        JObject tbox = JObject.Load(jsonReader);
    }
}

However, note that the RFC says, "The names within an object SHOULD be unique" so if you can, recommend the format be changed.

EDIT: Here's an alternate design that doesn't have duplicate keys:

[
    {
        "TBox": {
            "Width": 1,
            "Length": 1,
            "Name": "SmallBox",
            "Height": 2
        }
    },
    {
        "TBox": {
            "Width": 10,
            "Length": 5,
            "Name": "MedBox",
            "Height": 10
        }
    },
    {
        "TBox": {
            "Width": 20,
            "Length": 20,
            "Name": "LargeBox",
            "Height": 10
        }
    }
]

这篇关于Json.NET (Newtonsoft.Json) - 两个同名的“属性"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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