将Json对象转换为实体

时间:2016-10-25 12:32:20

标签: c# json

我想看看你如何在C#中构建实体以适应这个Json

的一些建议
{  
"activities-heart": [
    {
      "dateTime": "2016-10-25",
      "value": {
        "customHeartRateZones": [],
        "heartRateZones": [
          {
            "max": 88,
            "min": 30,
            "name": "Out of Range"
          },
          {
            "max": 123,
            "min": 88,
            "name": "Fat Burn"
          },
          {
            "max": 150,
            "min": 123,
            "name": "Cardio"
          },
          {
            "max": 220,
            "min": 150,
            "name": "Peak"
          }
        ]
      }
    }
  ],
  "activities-heart-intraday": {
    "dataset": [],
    "datasetInterval": 1,
    "datasetType": "minute"
  }
}

This Site建议:

public class HeartRateZone
{
    public int max { get; set; }
    public int min { get; set; }
    public string name { get; set; }
}

public class Value
{
    public List<object> customHeartRateZones { get; set; }
    public List<HeartRateZone> heartRateZones { get; set; }
}

public class ActivitiesHeart
{
    public string dateTime { get; set; }
    public Value value { get; set; }
}

public class ActivitiesHeartIntraday
{
    public List<object> dataset { get; set; }
    public int datasetInterval { get; set; }
    public string datasetType { get; set; }
}

public class RootObject
{
    public List<ActivitiesHeart> __invalid_name__activities-heart { get; set; }
    public ActivitiesHeartIntraday __invalid_name__activities-heart-intraday { get; set; }
}

我的问题是,它不会返回任何内容,它会尝试构建实体但它们为null。是否存在转换器和我看不到的东西。

2 个答案:

答案 0 :(得分:2)

假设您使用NewtonSoft反序列化json(并且非常确定以下内容对其他反序列化器也是正确的)问题出在您的类上。

如果您的课程我运行以下行,如您所述,我在属性中获得了null值。

var result = JsonConvert.DeserializeObject<RootObject>(jsonString);

现在,如果你看看你有的类,他们甚至不编译

public class RootObject
{
    public List<ActivitiesHeart> __invalid_name__activities-heart { get; set; }
    public ActivitiesHeartIntraday __invalid_name__activities-heart-intraday { get; set; }
}

该网站甚至告诉您, 属性名称无效 __invalid_name__activities-heart。您不能在其中包含-的媒体资源名称..

要解决此问题,您需要:

  1. 将类中的属性名称更改为有效名称+使用新名称更新json:

    public class RootObject
    {
        public List<ActivitiesHeart> ActivitiesHeart { get; set; }
        public ActivitiesHeartIntraday ActivitiesHeartIntraday { get; set; }
    }
    
  2. 或者更好的是,更改属性名称,然后Jim建议使用JsonPropertyAttribute

    public class RootObject
    {
        [JsonProperty("activities-heart")]
        public List<ActivitiesHeart> ActivitiesHeart { get; set; }
        [JsonProperty("activities-heart-intraday")]
        public ActivitiesHeartIntraday ActivitiesHeartIntraday { get; set; }
    }
    
  3. 它会起作用

答案 1 :(得分:0)

Thnx与JsonProperty一起工作。

下面是Icall api和反序列化的方式,就像其他人需要帮助一样。

var apiCall = ToFullUrl("/1/user/{0}/activities/heart/date/{1}/{2}.json", encodedUserId, ToFitbitFormat(startDate), ToFitbitFormat(endDateOrPeriod));
HttpResponseMessage response = await HttpClient.GetAsync(apiCall);
await HandleResponse(response);string responseBody = await response.Content.ReadAsStringAsync();
RootObject entity = JsonConvert.DeserializeObject<RootObject>(responseBody);