自定义JSon转换为C#对象
我从第三方API获取JSon,该API与我的类不匹配。自定义JSon转换为C#对象
一些JSON属性不能被转换别人有不同的名称等
我如何定义从JSON的自定义转换到我的C#对象。
这是我的对象在C#:
public class PropertyList {
public Int32 Page { get; set; }
public String[] ErrorMessages { get; set; }
public List<Property> Properties { get; set; }
}
public class Property {
public String Name { get; set; }
public String Reference { get; set; }
public String Region { get; set; }
public IList<String> Features { get; set; }
public String Id { get; set; }
public Double Price { get; set; }
public IList<String> ImagesUrls { get; set; }
}
这是我想从转换JSON数据:
{
"page" : 0,
"errorMessages" : [ ],
"listings" : [
{
"data" : {
"name" : "The name",
"reference__c" : "ref1234",
"region__c" : "London",
"features__c" : "Garage;Garden;",
"id" : "id1234",
"price_pb__c" : 700000,
},
"media" : {
"images" : [
{
"title" : "image1",
"url" : "http://www.domain.com/image1"
},
{
"title" : "image2",
"url" : "http://www.domain.com/image2"
}
]
}
}
{
NOTE: Other items
}
]
}
我应该怎么办呢?
更新1
使用蒂亚戈建议,我是能够解析页面和ErrorMessages而不是属性。所以,我创建了以下转换器:
public class PropertyResultConverter : JsonConverter {
public override bool CanConvert(Type objectType) {
return (objectType == typeof(PropertyResult));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
JObject jo = JObject.Load(reader);
PropertyResult propertyResult = jo.ToObject<PropertyResult>();
propertyResult.Properties = jo.SelectToken("listings.data").ToObject<List<Property>>();
return propertyResult;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
throw new NotImplementedException();
}
}
但是当我尝试这一点,我得到以下错误: 型“System.NullReferenceException”的发生异常
在:
propertyResult.Properties = jo.SelectToken("listings.data").ToObject<List<Property>>();
任何为什么发生这种情况?
米格尔,你试过http://www.newtonsoft.com/json?
您可以使用C#属性来完成代码的映射,如下面的代码片段所示。
public class PropertyList {
[JsonProperty("page")]
public Int32 Page { get; set; }
[JsonProperty("errorMessages")]
public String[] ErrorMessages { get; set; }
[JsonProperty("listings")]
public List<Property> Properties { get; set; }
}
public class Property {
[JsonProperty("name")]
public String Name { get; set; }
[JsonProperty("reference__c")]
public String Reference { get; set; }
[JsonProperty("region__c")]
public String Region { get; set; }
[JsonProperty("features__c")]
public IList<String> Features { get; set; }
[JsonProperty("id")]
public String Id { get; set; }
[JsonProperty("price_pb__c")]
public Double Price { get; set; }
[JsonProperty("media")]
public IList<String> ImagesUrls { get; set; }
}
我不确定IList是否会按照您的预期工作。我相信你将需要适应它的字典。
您可以在API一看: http://www.newtonsoft.com/json/help/html/SerializationAttributes.htm
UPDATE
尝试使用下面的代码片段解析 '数据':
JObject jsonObject = JObject.Parse(json);
IList<JToken> results = jsonObject["listings"]["data"].Children().ToList();
IList<Property> processedList = new List<Property>();
foreach (JToken item in results)
{
Property propertyItem = JsonConvert.DeserializeObject<Property>(item.ToString());
processedList.Add(propertyItem);
}
我跑了段和它的工作。您可以探索,我在我的github上生成的代码:
我试过你的建议,但我仍然有解析属性的问题。然后我尝试了一个自定义转换器,但没有运气我刚刚添加了一个更新。你知道什么可能会出错吗? –
Miguel,我在这里尝试,我很快就会发布答案。 –
是我的更新有用吗? –