且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Json.NET(Newtonsoft.Json) - 具有相同名称的两个“属性”

更新时间:2022-11-17 17:09:33

 使用Newtonsoft.Json; 
使用Newtonsoft.Json.Linq;

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

但是,请注意, RFC 说,对象中的名称应该是唯一的,所以如果可以的话,建议更改格式。 / p>

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

  [
{
TBox:{
宽度:1,
长度:1,
名称:SmallBox b $ b高度:2
}
},
{
TBox:{
宽度:10,
长度 :5,
名称:MedBox,
高度:10
}
},
{
TBox:{
宽度:20,
长度:20,
名称:大框,
高度: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
        }
    }
]