2014-02-26 51 views
0

我有一個叫做JSON網絡服務,並得到結果在C#.The JSON網絡服務數據從對象列表中的數值格式可供選擇:如何獲得在C#

{ 
    "Count": 9862, 
    "Items": [ 
    { 
     "Admin": { 
     "S": "false" 
     }, 
     "UserId": { 
     "S": "e9633477-978e-4956-ab34-cc4b8bbe4adf" 
     }, 
     "Age": { 
     "N": "76.24807963806055" 
     }, 
     "Promoted": { 
     "S": "true" 
     }, 
     "UserName": { 
     "S": "e9633477" 
     }, 
     "Registered": { 
     "S": "true" 
     } 
    }, 
    { 
     "Admin": { 
     "S": "false" 
     }, 
     "UserId": { 
     "S": "acf3eff7-36d6-4c3f-81dd-76f3a8071bcf" 
     }, 
     "Age": { 
     "N": "64.79224276370684" 
     }, 
     "Promoted": { 
     "S": "true" 
     }, 
     "UserName": { 
     "S": "acf3eff7" 
     }, 
     "Registered": { 
     "S": "true" 
     } 
    }, 

我已經得到了響應像這樣在C#中:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/userdetails"); 
     try 
     { 
      WebResponse response = request.GetResponse(); 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
       return reader.ReadToEnd(); 
      } 
     } 

終於成功獲得響應後,我已經得到了所有的數據字符串。然後解析這個字符串在對象列表中。現在我有對象的列表,它顯示了調試中的計數。現在我想訪問像UserId的值:acf3eff7-36d6-4c3f-81dd-76f3a8071bcf像屬性。我不知道如何做到這一點。請幫助我,任何幫助將不勝感激。

+0

我認爲你正在尋找[JSON.net(http://json.codeplex.com/) –

+0

可能.....但我不知道如何獲得C#中的vaues。 – coolhimanshu

+0

下面是一個類似的問題:http://stackoverflow.com/questions/9010021/get-value-from-json-with-json-net – Praveen

回答

1

您可以使用下面的代碼從JSON獲得的值作爲對象:

 JObject obj = JObject.Parse(json); 
     int count = (int)obj["Count"]; 
     var Items = obj["Items"]; 
     foreach (var item in Items) 
       var admin = item["Admin"]; 
0

您正在將字符串反序列化爲c#對象。你需要創建一個代表json的對象。

例如 -

public class Admin 
    { 
    public string S { get; set; } 
    } 

    public class UserId 
    { 
    public string S { get; set; } 
    } 

    public class Age 
    { 
    public string N { get; set; } 
    } 

    public class Promoted 
    { 
    public string S { get; set; } 
    } 

    public class UserName 
    { 
    public string S { get; set; } 
    } 

    public class Registered 
    { 
    public string S { get; set; } 
    } 

    public class RootObject 
    { 
    public Admin Admin { get; set; } 
    public UserId UserId { get; set; } 
    public Age Age { get; set; } 
    public Promoted Promoted { get; set; } 
    public UserName UserName { get; set; } 
    public Registered Registered { get; set; } 
    } 

然後反序列化JSON字符串中使用jsonSerializer

JavaScriptSerializer serializer = new JavaScriptSerializer(); 
    var result = 
      (RootObject)serializer .DeserializeObject("Json String") 
-2
string json = @"{ 
    ""Name"": ""Apple"", 
    ""Expiry"": new Date(1230422400000), 
    ""Price"": 3.99, 
    ""Sizes"": [ 
     ""Small"", 
     ""Medium"", 
     ""Large"" 
    ] 
}"; 

JObject o = JObject.Parse(json); 

//This will be "Apple" 
string name = (string)o["Name"]; 
1

快速和骯髒方法:

//deserialize your string json using json.net 
    dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); 
    //get value of UserId in first Item 
    var UserId = jsonObj["Items"][0]["UserId"]["S"]; 

    //OR get the value of UserId for each Item in Items 
    //foreach(dynamic item in jsonObj["Items"]) 
     //item["UserId"]["S"]; 

的建議是使用C#對象通過@yousuf

1

提到要能夠訪問Json的財產像常見的C#對象的屬性,你需要反序列化JSON字符串強類型的對象(可以例如使用JSON.NET來進行反序列化)。

另一個方便的工具是http://json2csharp.com/。粘貼您的JSON有那麼你就可以生成類的定義是適用於JSON的自動映射:

//RootObject class definition generated using json2csharp.com 
//the rest of class definition removed for brevity. 
public class RootObject 
{ 
    public int Count { get; set; } 
    public List<Item> Items { get; set; } 
} 
........ 
........ 
//in main method 
var jsonString = .....; 
//deserialize json to strongly-typed object 
RootObject result = JsonConvert.DeserializeObject<RootObject>(jsonString); 
foreach(var item in result.Items) 
{ 
    //then you can access Json property like common object property 
    Console.WriteLine(item.UserId.S); 
} 
+0

JSON.NET的+1。這是下載最多的nuget軟件包的原因。 –