-2

我正在从 withings 获取测量结果,并希望在图表中显示它们,但无法将其转换为 json。我也尝试JsonConvert.SerializeObject(myString)使用Newtonsoft.dll简单

System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
sr.Serialize(myString);

但它没有转换。

我的 withings 测量字符串如下。

{
  "status": 0,
  "body": {
    "updatetime": 1392764547,
    "measuregrps": [
      {
        "grpid": 17945868,
        "attrib": 0,
        "date": 139984270,
        "category": 1,
        "measures": [
          {
            "value": 72,
            "type": 9,
            "unit": 0
          },
          {
            "value": 152,
            "type": 10,
            "unit": 7
          },
          {
            "value": 87,
            "type": 17,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 176587495,
        "attrib": 0,
        "date": 13915689,
        "category": 1,
        "measures": [
          {
            "value": 94,
            "type": 9,
            "unit": 0
          },
          {
            "value": 145,
            "type": 10,
            "unit": 0
          },
          {
            "value": 109,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179262494,
        "attrib": 0,
        "date": 1391369607,
        "category": 1,
        "measures": [
          {
            "value": 77,
            "type": 9,
            "unit": 0
          },
          {
            "value": 121,
            "type": 10,
            "unit": 0
          },
          {
            "value": 87,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179258492,
        "attrib": 0,
        "date": 1391171167,
        "category": 1,
        "measures": [
          {
            "value": 61,
            "type": 9,
            "unit": 0
          },
          {
            "value": 107,
            "type": 10,
            "unit": 0
          },
          {
            "value": 80,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179089150,
        "attrib": 0,
        "date": 1391167537,
        "category": 1,
        "measures": [
          {
            "value": 69,
            "type": 9,
            "unit": 0
          },
          {
            "value": 112,
            "type": 10,
            "unit": 0
          },
          {
            "value": 67,
            "type": 11,
            "unit": 0
          }
        ]
      },
      {
        "grpid": 179079661,
        "attrib": 2,
        "date": 1391164672,
        "category": 1,
        "measures": [
          {
            "value": 720,
            "type": 1,
            "unit": -1
          }
        ]
      },
      {
        "grpid": 17998560,
        "attrib": 2,
        "date": 146989672,
        "category": 1,
        "measures": [
          {
            "value": 284,
            "type": 4,
            "unit": -2
          }
        ]
      }
    ]
  }
}
4

2 回答 2

1

看来,你想反序列化你的 json 字符串,而不是序列化:

var obj = JsonConvert.DeserializeObject<Withings.RootObject>(json);

public class Withings
{
    public class Measure
    {
        public int value { get; set; }
        public int type { get; set; }
        public int unit { get; set; }
    }

    public class Measuregrp
    {
        public int grpid { get; set; }
        public int attrib { get; set; }
        public int date { get; set; }
        public int category { get; set; }
        public List<Measure> measures { get; set; }
    }

    public class Body
    {
        public int updatetime { get; set; }
        public List<Measuregrp> measuregrps { get; set; }
    }

    public class RootObject
    {
        public int status { get; set; }
        public Body body { get; set; }
    }
}
于 2014-02-18T19:07:03.090 回答
0

JsonConvert.SerializeObject(myString)接受一个对象并返回一个字符串。如果你想把一个字符串变成你想使用的对象Deserialize<T>(sting json)。给定示例中的参数名称,myString我会假设您使用的方法错误。

要反序列化,您需要一个等效类型,例如;

   public class myObject
   {
        public int status { get; set; }
        public Body body { get; set; } 
   } 
   public class Body
   {
            //other parameters ect
    }

您的对象模型需要与 json 完全匹配才能Deserialize<T>正确运行。

于 2014-02-18T18:30:20.220 回答