c# - 没有 id 的 Json Net $ref
问题描述
我想使用 $ref 指向属性而不是对象中的 id。
具体来说,在下面的示例中,我希望AlternateAdress指向Address1中的城市属性
String str3 = "{
'Name': 'Jon Smith',
'Address1': { '$id': '1', 'City': 'New York', 'State': 'NY'},
'AlternateAddress':{'$ref':'#/Address1/City'}}";
Person stuff = JsonConvert.DeserializeObject<Person>(str3, settings);
Console.WriteLine(stuff.AlternateAddress);
上面的代码返回错误
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path 'AlternateAddress', line 1, position 105.
at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Rextester.Program.Main(String[] args)
解决方案
推荐阅读
- python - InternalError_:频谱扫描错误。S3 到 Redshift 复制命令
- django - 无法使用自定义创建序列化程序 django rest_framework 保存数据
- python - 追加时更改了列名
- c# - 创建一个 C# 程序来计算给定数字的各个数字的总和
- rest - 使用 JAX-RS 如何将 API 服务返回的数据数组收集到 java 对象列表中?
- python - 如何将 Python 请求库与 IBMWatson API 一起使用?
- mysql - 是否所有外键都有一个使用 INSERT GUI 的下拉选项
- javascript - Web Audio API:高效播放 PCM 流
- php - Composer 无法要求 unisharp/laravel-filemanager
- r - 使用 apply() 时是否可以保留内存?