Newtonsoft DeserializeXNode错误地扩展了内部数组 [英] Newtonsoft DeserializeXNode expands internal arrays incorrectly

查看:59
本文介绍了Newtonsoft DeserializeXNode错误地扩展了内部数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的JSON对象:

I have a JSON object like the following:

{
  "property1": "value1",
  "property2": "value2",
  "property3": ["value3","value4","value5"]
}

但是,当我尝试使用DeserializeXNode转换为XML时,数组消失了.

When I try to use DeserializeXNode to convert to XML, though, the array goes away.

<MyObj>
    <property1>value1</property1>
    <property2>value2</property2>
    <property3>value3</property3>
    <property3>value4</property3>
    <property3>value5</property3>
</MyObj>

当我尝试重新序列化回对象时,这会导致问题,因为我收到无法将字符串转换为字符串[]"错误.此外,当我尝试解析文档时,重复的属性将被覆盖,仅保留最后一个值.我尝试将DeserializeXNode的第三个参数设置为真正的想法,该想法可以正确标记数组,但不会改变任何内容.

This causes issue when I try to re-serialize back to the object, because I get a "cannot convert string to string[]" error. Moreover, when I try to parse the document, the duplicate properties are overwritten and only the last value remains. I've tried setting the third parameter of DeserializeXNode to true thinking that would mark the array properly, but that doesn't change anything.

根据我对Newtonsoft文档的理解,XML应该更像:

By my understanding from the Newtonsoft docs, the XML should look more like:

<MyObj>
    <property1>value1</property1>
    <property2>value2</property2>
    <property3 type="array">
        <item>value3</item>
        <item>value4</item>
        <item>value5</item>
    </property3>
</MyObj>

我需要怎么做才能使数组正确转换为XML,以便将其作为数组返回到JSON?

What do I need to do to get the array to properly translate into the XML so that it will come back to JSON as an array?

作为参考,这是我正在使用的代码(继承,所以我不确定是否有意义):

For reference, here is the code I'm using (inherited, so I'm not sure if it makes sense):

ObjectToXML:

ObjectToXML:

var json = JsonConvert.SerializeObject(o);
var xdoc = JsonConvert.DeserializeXNode(json, "MyObj", true);
return xdoc.ToString();

XMLToObject:

XMLToObject:

XDocument d = XDocument.Parse(xml, LoadOptions.None);
d.Descendants().ForEach(e => e.Attributes().Remove());
var json = JsonConvert.SerializeXNode(d);
json = json.Replace("\"?xml\":{\"@version\":\"1.0\",\"@encoding\":\"utf-16\"},\"MyObj\":{", "")
    .Replace("{\"MyObj\":", "")
    .Replace("}}", "}")
    .Replace("{\"anyType\":", "")
    .Replace("]}", "]");
return JsonConvert.DeserializeObject<T>(json);

对象本身:

public class MyObj
{
    public string property1 { get; set; }
    public string property2 { get; set; }
    public string[] property3 { get; set; }
}

推荐答案

我不确定您在

I'm not sure where you've read this in the documentation. I've looked and I can't find anything that suggests the XML should look as you've suggested.

此方法的工作原理是可以将具有相同名称的多个元素推断为数组.从文档中:

How this works is that multiple elements with the same name can be inferred to be an array. From the docs:

在同一级别上具有相同名称的多个节点被分组到一个数组中.

Multiple nodes with the same name at the same level are grouped together into an array.

如果只有一个元素源自数组,那么您正在使用的重载会指示序列化程序添加一个指示该元素为数组的属性.您的输出将如下所示:

And if there is only one element that originated from an array, then there is an overload you're using that instructs the serialiser to add an attribute indicating that element is an array. Your output would look something like this:

<MyObj xmlns:json="http://james.newtonking.com/projects/json">
  <property1>value1</property1>
  <property2>value2</property2>
  <property3 json:Array="true">value3</property3>
</MyObj>

如果您使用以下代码通过XML进行往返:

And if you round trip it via XML using this code:

var asXml = JsonConvert.DeserializeXNode(json, "MyObj", writeArrayAttribute: true);
var asJson = JsonConvert.SerializeXNode(asXml, Formatting.Indented, omitRootObject: true);

您将获得完全相同的JSON.所以我在这里找不到问题.

You get exactly the same JSON back. So I can't find a problem here.

您可以在此小提琴中看到结果,包括反序列化为MyObj.

You can see the results in this fiddle, including deserialising to MyObj.

这篇关于Newtonsoft DeserializeXNode错误地扩展了内部数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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