2017-09-19 228 views
-5

我的JSON輸入如下。 Sry它太長了解析C中的對象數組JSON#

{ 
    "status":{ 
     "success":[ 
     { 
      "User":{ 
       "id":"1377", 
       "username":"Dr.Hema Sathish", 
       "username_url":"dr-hema-sathish", 
       "firstname":null, 
       "lastname":null, 
       "email":"[email protected]", 
       "password":"6c7ab07e828828206e3d7c56c3c35cfd383960cd", 
       "user_level":"doctor" 
      }, 
      "Speciality":{ 
       "id":"2", 
       "name":"Dermatology(Skin Specialist)", 
       "slug":"dermatology-skin-specialist", 
       "image":"1438693213.png", 
       "status":"1", 
       "created":"2015-07-08 03:35:57", 
       "modified":"2016-09-13 13:58:36" 
      } 
     } 
     ] 
    } 
} 

我已經從URL下載成功下載了JSON字符串。

static void main 
{ 
    using (var webClient = new System.Net.WebClient()) 
    { 
     string json = webClient.DownloadString("http://myURL/"); 
     Console.WriteLine(json.ToString()); 
    } 
} 

現在我必須得到用戶和專業對象。它令人困惑的解析對象數組Json。如何解析它?

我試過反序列化。但它不起作用。

如何解析它?

+0

您是否在'json c#'上檢查過搜索引擎?您應該使用庫 –

+1

請提供您已嘗試過的代碼示例 –

+3

我覺得今天將成爲JSON轉換日,每小時我們會得到這樣的問題。 – HimBromBeere

回答

0

您的JSON是無效...這是正確的JSON

{ 
    "status": { 
     "success": [{ 
       "User": { 
        "id": "1377", 
        "username": "Dr.Hema Sathish" 
       }, 
       "Speciality": { 
        "id": "2", 
        "name": "Dermatology(Skin Specialist)" 
       } 
      }, 
      { 
       "User": { 
        "id": "1390", 
        "username": "Dr.Nichita Balaji" 
       }, 
       "Speciality": { 
        "id": "2", 
        "name": "Dermatology(Skin Specialist)" 
       } 

      } 
     ] 
    } 
    } 

您可以創建下面的類使用Newtonsoft

到反序列化
public class User 
{ 
    public string id { get; set; } 
    public string username { get; set; } 
} 

public class Speciality 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
} 

public class Success 
{ 
    public User User { get; set; } 
    public Speciality Speciality { get; set; } 
} 

public class Status 
{ 
    public List<Success> success { get; set; } 
} 

public class RootObject 
{ 
    public Status status { get; set; } 
} 

下面是代碼以反序列化

string json = 
     " {\r\n \t\"status\": {\r\n \t\t\"success\": [{\r\n \t\t\t\t\"User\": {\r\n \t\t\t\t\t\"id\": \"1377\",\r\n \t\t\t\t\t\"username\": \"Dr.Hema Sathish\"\r\n \t\t\t\t},\r\n \t\t\t\t\"Speciality\": {\r\n \t\t\t\t\t\"id\": \"2\",\r\n \t\t\t\t\t\"name\": \"Dermatology(Skin Specialist)\"\r\n \t\t\t\t}\r\n \t\t\t},\r\n \t\t\t{\r\n \t\t\t\t\"User\": {\r\n \t\t\t\t\t\"id\": \"1390\",\r\n \t\t\t\t\t\"username\": \"Dr.Nichita Balaji\"\r\n \t\t\t\t},\r\n \t\t\t\t\"Speciality\": {\r\n \t\t\t\t\t\"id\": \"2\",\r\n \t\t\t\t\t\"name\": \"Dermatology(Skin Specialist)\"\r\n \t\t\t\t}\r\n\r\n \t\t\t}\r\n \t\t]\r\n \t}\r\n }"; 
    RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(json); 
    var successes = rootObject.status.success; 
    var specialityObjects = successes.Select(success => success.Speciality).ToList(); 
    var users = successes.Select(success => success.User).ToList(); 
    Console.WriteLine(users[0].id + " " + users[0].username); 
    Console.WriteLine(users[1].id + " " + users[1].username); 
    Console.WriteLine(specialityObjects[0].id + " " + specialityObjects[0].name); 
    Console.WriteLine(specialityObjects[1].id + " " + specialityObjects[1].name); 
    Console.ReadLine(); 

//輸出

1377 Dr.Hema Sathish所在 1390 Dr.Nichita巴拉吉 2皮膚病(皮膚專家) 2皮膚科(皮膚專家)

+0

如何顯示用戶和專業對象。我已經嘗試過,如你所示。但是NullReferenceException顯示在這一行中。 Console.Writeln(User.id); – arthy

+0

如果我嘗試訪問用戶詳細信息使用Console.WriteLine(tmp.status.success [0] .User);它只在輸出中顯示filename.user。 – arthy

+0

@arthy ...更新了我的答案...它的LINQ訪問對象...不要忘記標記爲答案,如果它幫助你 – Abhay

0

你必須根據JSON數據集來創建類:

public class User 
{ 
    public int Id { get; set; } 
    public string Username { get; set; } 
    public string Firstname { get; set; } 
    public string Lastname { get; set; } 
} 

public class Speciality 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

然後,你應該能夠使用JSON .NET庫反序列化

string json = webClient.DownloadString("http://myURL/"); 
User u = JsonConvert.DeserializeObject<User>(json); 

可能有其他圖書館提供,仍然this one工作正常(我以NuGET包的形式提供)。

請注意,這只是一個示例,您的代碼將錯過幾個類來正確解析您的JSON(Status類等)。