无法反序列化包含$ REF键JSON [英] Can not deserialize JSON containing $ref keys

查看:491
本文介绍了无法反序列化包含$ REF键JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码尝试反序列化一个JSON字符串和图书馆给了我这个错误:




其他内容JSON参考发现目的。一个JSON引用对象应该只有一个$ REF属性。路径user.obj,第1行,第34位。




任何想法有什么不好? (据我所知,这是抱怨的第二个$裁判,但我不知道为什么。)什么解决办法?



<预类=郎-CS prettyprint,覆盖> 无效的主要()
{
变种s ={\user\:{\$ ref\:\123456\ \​​obj\:{\$ ref\:\123456\}}};
JsonConvert.DeserializeObject&所述;根和将(S)使用.dump();
}

//定义其它的方法和类在这里
公共类根
{
[JsonProperty(用户)]
公用户用户{搞定;组; }
}

公共类用户
{
[JsonPropertyAttribute($ REF)
公共字符串编号{搞定;组; }

[JsonPropertyAttribute(目标文件)]
公开的OBJ的OBJ {搞定;组; }
}

公共类的OBJ
{
[JsonPropertyAttribute($ REF)
公共字符串编号{搞定;组; }
}


解决方案

Json.Net使用 $ REF 的$ id 作为元数据保存在JSON对象的引用。它看到因此,当 $ REF 它假定属性不是实际的JSON属性集的一部分,但内部标识符指的是匹配的的$ id 其他地方的JSON。由于您的 $的使用REF 是比Json.Net希望看到,它抛出一个错误不同。



更新



在Json.Net版本的 6.0.4 以后,现在有,通过它可以指示解串器来治疗设置这些元数据属性为正常属性而不是消耗他们。所有你需要做的是将 MetadataPropertyHandling 设置为忽略,然后反序列化和往常一样。

  VAR设置=新JsonSerializerSettings(); 
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

VAR OBJ = JsonConvert.DeserializeObject< FormDefinitionList>(JSON,设置);

在此之前6.0.4版本,需要一种变通方法来解决这个问题。如果您无法升级到Json.Net的最新版本,请参见我的回答给了一些可能的解决方案类似的问题


I have the following code trying to deserialize a JSON string and the library gives me this error:

Additional content found in JSON reference object. A JSON reference object should only have a $ref property. Path 'user.obj', line 1, position 34.

Any idea what is wrong? (I understand that it is complaining about the second $ref, but I don't know why.) What is the workaround ?

void Main()
{
    var s = "{\"user\": {\"$ref\": \"123456\", \"obj\": {\"$ref\": \"123456\"}}}";
    JsonConvert.DeserializeObject<Root>(s).Dump();
}

// Define other methods and classes here
public class Root
{
    [JsonProperty("user")]
    public User User { get; set; }
}

public class User
{
    [JsonPropertyAttribute("$ref")]
    public string Ref { get; set; }

    [JsonPropertyAttribute("obj")]
    public Obj Obj { get; set; }
}

public class Obj
{
    [JsonPropertyAttribute("$ref")]
    public string Ref { get; set; }
}

解决方案

Json.Net uses $ref along with $id as metadata to preserve object references in JSON. So when it sees $ref it assumes that property is not part of the actual JSON property set, but an internal identifier referring to a matching $id somewhere else in the JSON. Since your usage of $ref is different than what Json.Net expects to see, it is throwing an error.

UPDATE

In Json.Net version 6.0.4 and later, there is now a setting by which you can instruct the deserializer to treat these metadata properties as normal properties instead of consuming them. All you need to do is set the MetadataPropertyHandling setting to Ignore and then deserialize as usual.

var settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;

var obj = JsonConvert.DeserializeObject<FormDefinitionList>(json, settings);

Prior to version 6.0.4, a workaround was needed to solve this issue. If you cannot upgrade to the lastest version of Json.Net, see my answer to a similar question for some possible solutions.

这篇关于无法反序列化包含$ REF键JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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