【问题标题】:Not able to convert string to json无法将字符串转换为 json
【发布时间】:2014-02-18 18:21:33
【问题描述】:

我正在从 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
          }
        ]
      }
    ]
  }
}

【问题讨论】:

  • 会发生什么?您收到错误消息吗?您是否尝试过 JSON 的较小/子集?
  • 不,我没有收到错误,但输出与输入相同
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# asp.net json json.net withings


【解决方案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; }
    }
}

【讨论】:

    【解决方案2】:

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

    要反序列化,你需要一个等价的类型;

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

    您的对象模型需要按照Deserialize&lt;T&gt; 的顺序与 json 完全匹配才能正常运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2016-10-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多