C# JSON.net:将一个对象转换为两个独立的数组,而不使用父对象–;使用JsonConverter
JSON.net:如何使用JsonConverter在没有父节点的情况下将1个对象转换/拆分为2个单独的数组 JSON–实际的:C# JSON.net:将一个对象转换为两个独立的数组,而不使用父对象–;使用JsonConverter,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,JSON.net:如何使用JsonConverter在没有父节点的情况下将1个对象转换/拆分为2个单独的数组 JSON–实际的: {"Name":"Bus", "Type":"Vehicle", "SymCollection":[ {"ids":[0,1]}, {"weights":[100,50]} ]} {"Name":"Bus", "Type":"Vehicle", "ids":[0,1], "weights":[100,50] } class TestCla
{"Name":"Bus",
"Type":"Vehicle",
"SymCollection":[
{"ids":[0,1]},
{"weights":[100,50]}
]}
{"Name":"Bus",
"Type":"Vehicle",
"ids":[0,1],
"weights":[100,50]
}
class TestClass
{
public string Name;
public string Type;
public SymCollection SymCollection;
}
[JsonConverter(typeof(SymCollectionConverter))]
class SymCollection
{
public int[] Ids;
public int[] Weights;
}
JSON–应为:
{"Name":"Bus",
"Type":"Vehicle",
"SymCollection":[
{"ids":[0,1]},
{"weights":[100,50]}
]}
{"Name":"Bus",
"Type":"Vehicle",
"ids":[0,1],
"weights":[100,50]
}
class TestClass
{
public string Name;
public string Type;
public SymCollection SymCollection;
}
[JsonConverter(typeof(SymCollectionConverter))]
class SymCollection
{
public int[] Ids;
public int[] Weights;
}
C#–源对象(无法更改):
{"Name":"Bus",
"Type":"Vehicle",
"SymCollection":[
{"ids":[0,1]},
{"weights":[100,50]}
]}
{"Name":"Bus",
"Type":"Vehicle",
"ids":[0,1],
"weights":[100,50]
}
class TestClass
{
public string Name;
public string Type;
public SymCollection SymCollection;
}
[JsonConverter(typeof(SymCollectionConverter))]
class SymCollection
{
public int[] Ids;
public int[] Weights;
}
C#–JsonConverter.如何更改它
class SymCollectionConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
SymCollection inputContainer = (SymCollection)value;
// Need to delete next line – do not need any parent node.
writer.WriteStartArray();
writer.WriteStartObject();
writer.WritePropertyName("ids");
writer.WriteStartArray();
foreach (var id in inputContainer.Ids)
{
writer.WriteValue(id);
}
writer.WriteEndArray();
writer.WriteEndObject();
writer.WriteStartObject();
writer.WritePropertyName("weights");
writer.WriteStartArray();
foreach (var weight in inputContainer.Weights)
{
writer.WriteValue(weight);
}
writer.WriteEndArray();
writer.WriteEndObject();
// So, and delete next line too.
writer.WriteEndArray();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return typeof(SymCollection) == objectType;
}
}
不幸的是,TestClass和SymCollection的结构无法更改
更新1:提出的可能解决方案之一,部分手工制作,无需JsonConverter:
您可以尝试使用
json.Linq
重写json。将值解析为,然后枚举SymCollection
项(因为它是一个数组),然后使用的Name
和value
遍历每个项属性并将每个属性添加到父项。最后删除SymCollection
token
var jObject=jObject.Parse(json);
foreach(JObject[“符号集合”]中的JObject项)
{
foreach(item.Properties()中的var属性)
{
添加(property.Name、property.Value);
}
}
jObject.Remove(“SymCollection”);
var result=jObject.ToString();
也许,内部for
循环是冗余的,您可以只获取每个SymCollection
项的第一个属性,而不使用循环(如果您确定的话)
上面的代码生成以下输出
{
"Name": "Bus",
"Type": "Vehicle",
"ids": [
0,
1
],
"weights": [
100,
50
]
}
您是否将
JsonConverter
视为此处的唯一选项?Json.Linq
对您有意义吗?删除您期望的Json
@Sajid最后一行中的]
,谢谢,修复了@PavelAnikhouski请您发布一个简单的示例,说明如何使用LINQ修复它?TestClass背后真正的类是巨大的,所以只修复SymCollection真的很好。@Olegzarevenny我已经添加了一个答案,请看一看,老实说,我希望有更好更简单的方法,但它帮助解决了这个问题。从某种意义上说,它是半手工制作的。我已经根据您对我的问题的回答添加了解决方案(请参阅:更新1)。@OlegZarevennyi好的解决方案!但就个人而言,我希望避免混合使用JsonConverter
和Json.Linq
。您可以轻松地将JToken
/JObject
转换为特定类型的实例并返回。无论如何,一切都取决于您的需求和现有的代码库