反序列化嵌套JSON结构扁平类使用注释Json.NET [英] Deserializing nested JSON structure to a flattened class with Json.NET using annotations

查看:169
本文介绍了反序列化嵌套JSON结构扁平类使用注释Json.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使用JsonProperty注释嵌套的Json属性映射到一个非嵌套.NET会员?说你有一些的Json是这样的:

  {
     ID:9999,
     CREATED_DATE:星期四,2011年6月23日12时56分24秒+0000
     POS:{
        类型:SOMETYPE,
        坐标:[
           59.323,
           18.0654
        ]
     }
}

和想用它反序列化到扁平MyClass类

  JsonConvert.DeserializeObject< MyClass的>(jsonstr);

注释可以被用来映射JSON的在下面的类坐标列表中纬度和LNG:

 公共类MyClass的{
   [JsonProperty(ID)]
   公众诠释标识{搞定;组; }
   [JsonProperty(CREATED_DATE)]
   公众的DateTime创建{搞定;组; }
   [JsonProperty(????)]
   公众持股量的纬度{搞定;组; }
   [JsonProperty(?????)]
   公众持股量的LNG {搞定;组; }
}

只是好奇。我总是可以这样定义类,它似乎很好地工作:

 公共类MyClass的{
    [JsonProperty(ID)]
    公众诠释标识{搞定;组; }
    [JsonProperty(DATE_CREATED)]
    公众的DateTime创建{搞定;组; }
    [JsonProperty(POS)]
    公共PosClass波什得到{;组; }
}公共类PosClass
{
    公开名单<浮动>坐标{搞定;组; }
}


解决方案

从个人的经验,我想重新使用我的实体通信(JSON,XML ...等)之前,但密切地关注现有的挣扎后,图案,我发现有数据传输对象除了内部/存储实体,你已经有将解放我的沟通模式,只有我付出的代价是接受上工,还没有直接的,两者之间manually- codeD转换的工作。

如果你宁愿坚持你所拥有的性能也没有什么大不了的,那么.NET反射是你的朋友。

Is it possible to use JsonProperty annotation to map a nested Json property to a non-nested .NET member? Say you've got some Json like this:

{
     "id":9999,
     "created_date":"Thu, 23 Jun 2011 12:56:24 +0000",
     "pos":{
        "type":"someType",
        "coordinates":[
           59.323,
           18.0654
        ]
     }
}

and want to deserialize it into a flattened class MyClass using

JsonConvert.DeserializeObject<MyClass>(jsonstr);

Can annotations be used to map the Json coordinates list to Lat and Lng in the class below:

public class MyClass {
   [JsonProperty("id")]
   public int Id { get; set; }
   [JsonProperty("created_date")]
   public DateTime Created { get; set; }
   [JsonProperty("????")]
   public float Lat { get; set; }
   [JsonProperty("?????")]
   public float Lng { get; set; }
}

Just curious. I can always define the class like this and it seems to work fine:

public class MyClass {
    [JsonProperty("id")]
    public int Id { get; set; }
    [JsonProperty("date_created")]
    public DateTime Created { get; set; }
    [JsonProperty("pos")]
    public PosClass Pos { get; set; }
}

public class PosClass
{
    public List<float> coordinates { get; set; }
}

解决方案

From personal experience, I struggled before trying to re-use my entities for communication (JSON, XML ... etc.) but after paying closer attention to existing patterns, I found out that having "data transfer objects" in addition to internal / storage entities that you already have will liberate my communication models and the only cost I paid was to accept doing manual, yet straight-forward, effort of manually-coded conversion between the two.

If you'd rather stick to what you have and performance is no big deal, then .NET reflection is your friend.

这篇关于反序列化嵌套JSON结构扁平类使用注释Json.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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