2014-02-18 233 views
-2

我從withings獲取度量值,並希望在圖中顯示它們,但無法將其轉換爲json。我也嘗試使用JsonConvert.SerializeObject(myString)Newtonsoft.dll簡單無法將字符串轉換爲json

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

但不轉換。

我的腰帶測量字符串如下。

{ 
    "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 
      } 
     ] 
     } 
    ] 
    } 
} 
+0

會發生什麼?你有錯誤信息嗎?您是否嘗試過JSON的更小/子集? – n8wrl

+0

不,我沒有得到錯誤,但輸入相同的輸出 –

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

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; } 
    } 
} 
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 
    } 

你的對象模型需要通過準確的Deserialize<T> JSON匹配才能正常運行。