更新 JObject 中的 JArray JSON.NET
Update JArray in JObject JSON.NET
我有两个文件,一个是json,另一个是xml,我需要合并这两个文件,我决定在json之后转换xml 39=].
{
"Level1": {
"Level2": [
{
"id": "Chart",
"Box": [
{
"id": "1",
"value": "10"
},
{
"id": "2",
"value": "20"
}
]
}
]
}
}
第二个Json:
{
"Level1": {
"Level2": [
{
"id": "NameApp",
"Box": [
{
"id": "2",
"value": "90"
},
{
"id": "3",
"value": "50"
}
]
}
]
}
}
输出:
{
"Level1": {
"Level2": [
{
"id": "Chart",
"Box": [
{
"id": "1",
"value": "10"
},
{
"id": "2",
"value": "20"
}, {
"id": "2",
"value": "90"
},
{
"id": "3",
"value": "50"
}
]
}
]
}
}
XML代码:
XmlDocument doc = new XmlDocument();
doc.Load(pathXml);
doc.RemoveChild(doc.FirstChild);
string jsonTextXml = JsonConvert.SerializeXmlNode(doc);
JSON代码:
using (StreamReader readerJson = new StreamReader(pathJson))
{
jsonTextJson = readerJson.ReadToEnd();
}
合并代码:
JObject o1 = JObject.Parse(jsonTextJson);
JObject o2 = JObject.Parse(jsonTextXml);
JArray box1 = o1["Level1"]["Level2"]["Box"][0] as JArray;
JArray box2 = o2["Level1"]["Level2"]["Box"][0] as JArray;
box1 = new JArray(box1.Concat(box2));
o1["Level1"]["Level2"]["Box"][0].Replace(box1);
当我想要获取 box1 时,出现此错误:未将对象引用设置为对象实例。
我用另一种方式测试过..
JArray box1 = o1["Level1"]["Level2"][0]["Box"] as JArray;
怎么了?
最后这是我的解决方案解决方案:
public string joinJson(string jsonFinal, string jsonTemp)
{
JObject jsonMaster = JObject.Parse(jsonFinal);
JObject jsonForMerge = JObject.Parse(jsonTemp);
foreach (var element in jsonForMerge["Level1"]["Level2"])
{
string pathElement = element.Path;
string component = pathElement.Split(new char[] { '.' }).Last();
if (element.HasValues && !component.Equals("id"))
{
JArray contentTemp = jsonForMerge["Level1"]["Level2"][component] as JArray;
JArray contentFinal = jsonMaster["Level1"]["Level2"][0][component] as JArray;
contentFinal = new JArray(contentFinal.Concat(contentTemp));
jsonMaster["Level1"]["Level2"][0][component].Replace(contentFinal);
}
}
return jsonMaster.ToString();
}
我建议反过来做。将您的 json 反序列化为 类(这可能会有所帮助:JsonUtils)。
public class Box
{
public string id { get; set; }
public string value { get; set; }
}
public class Level2
{
public string id { get; set; }
public List<Box> Box { get; set; }
}
public class Level1
{
public List<Level2> Level2 { get; set; }
}
public class RootObject
{
public Level1 Level1 { get; set; }
}
这是您的两个 json 字符串的结构。
现在反序列化:
RootObject rootObject1 = JsonConvert.DeserializeObject<RootObject>(json1);
RootObject rootObject2 = JsonConvert.DeserializeObject<RootObject>(json2);
现在您可以遍历您的对象并'merge'它然后再次序列化它们。
foreach (var level in rootObject2.Level1.Level2)
{
rootObject1.Level1.Level2.Add(level);
}
var json = JsonConvert.SerializeObject(rootObject1);
当然,您可以将对象合并得更深(对于 Box 中的每个框到 Level2)。
我有两个文件,一个是json,另一个是xml,我需要合并这两个文件,我决定在json之后转换xml 39=].
{
"Level1": {
"Level2": [
{
"id": "Chart",
"Box": [
{
"id": "1",
"value": "10"
},
{
"id": "2",
"value": "20"
}
]
}
]
}
}
第二个Json:
{
"Level1": {
"Level2": [
{
"id": "NameApp",
"Box": [
{
"id": "2",
"value": "90"
},
{
"id": "3",
"value": "50"
}
]
}
]
}
}
输出:
{
"Level1": {
"Level2": [
{
"id": "Chart",
"Box": [
{
"id": "1",
"value": "10"
},
{
"id": "2",
"value": "20"
}, {
"id": "2",
"value": "90"
},
{
"id": "3",
"value": "50"
}
]
}
]
}
}
XML代码:
XmlDocument doc = new XmlDocument();
doc.Load(pathXml);
doc.RemoveChild(doc.FirstChild);
string jsonTextXml = JsonConvert.SerializeXmlNode(doc);
JSON代码:
using (StreamReader readerJson = new StreamReader(pathJson))
{
jsonTextJson = readerJson.ReadToEnd();
}
合并代码:
JObject o1 = JObject.Parse(jsonTextJson);
JObject o2 = JObject.Parse(jsonTextXml);
JArray box1 = o1["Level1"]["Level2"]["Box"][0] as JArray;
JArray box2 = o2["Level1"]["Level2"]["Box"][0] as JArray;
box1 = new JArray(box1.Concat(box2));
o1["Level1"]["Level2"]["Box"][0].Replace(box1);
当我想要获取 box1 时,出现此错误:未将对象引用设置为对象实例。
我用另一种方式测试过..
JArray box1 = o1["Level1"]["Level2"][0]["Box"] as JArray;
怎么了?
最后这是我的解决方案解决方案:
public string joinJson(string jsonFinal, string jsonTemp)
{
JObject jsonMaster = JObject.Parse(jsonFinal);
JObject jsonForMerge = JObject.Parse(jsonTemp);
foreach (var element in jsonForMerge["Level1"]["Level2"])
{
string pathElement = element.Path;
string component = pathElement.Split(new char[] { '.' }).Last();
if (element.HasValues && !component.Equals("id"))
{
JArray contentTemp = jsonForMerge["Level1"]["Level2"][component] as JArray;
JArray contentFinal = jsonMaster["Level1"]["Level2"][0][component] as JArray;
contentFinal = new JArray(contentFinal.Concat(contentTemp));
jsonMaster["Level1"]["Level2"][0][component].Replace(contentFinal);
}
}
return jsonMaster.ToString();
}
我建议反过来做。将您的 json 反序列化为 类(这可能会有所帮助:JsonUtils)。
public class Box
{
public string id { get; set; }
public string value { get; set; }
}
public class Level2
{
public string id { get; set; }
public List<Box> Box { get; set; }
}
public class Level1
{
public List<Level2> Level2 { get; set; }
}
public class RootObject
{
public Level1 Level1 { get; set; }
}
这是您的两个 json 字符串的结构。 现在反序列化:
RootObject rootObject1 = JsonConvert.DeserializeObject<RootObject>(json1);
RootObject rootObject2 = JsonConvert.DeserializeObject<RootObject>(json2);
现在您可以遍历您的对象并'merge'它然后再次序列化它们。
foreach (var level in rootObject2.Level1.Level2)
{
rootObject1.Level1.Level2.Add(level);
}
var json = JsonConvert.SerializeObject(rootObject1);
当然,您可以将对象合并得更深(对于 Box 中的每个框到 Level2)。