2016-11-11 49 views
-4

我試圖讓下面的JSON:firstlast我解析的JSON像這樣:解析用C#多陣列

public class TwitterName 
    { 
     public string results.name.first { get; set; } 
     public string results.name.last { get; set; } 
     public override string ToString() 
     { 
      return string.Format("first: {0} last: {1}",first,last); 
     } 
    } 


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     //get the website with the random generated info 
     WebRequest req = WebRequest.Create("https://randomuser.me/api/"); 

     //get the response 
     HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 


     //read the info 
     using(StreamReader st = new StreamReader(res.GetResponseStream())) 
     { 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      var objText = st.ReadToEnd(); 
      TwitterName tn = js.Deserialize<TwitterName>(objText); 

      MessageBox.Show(tn.ToString()); 
     } 
    } 

,我沒有得到一個結果返回,以下JSON:

{"results":[{"gender":"female","name":{"title":"ms","first":"teresa","last":"martinez"},"location":{"street":"4896 avenida de andalucía","city":"santander","state":"aragón","postcode":42314},"email":"[email protected]","login":{"username":"redcat339","password":"dang","salt":"ApZZFECd","md5":"6d928ee42d64390a46e94172fce95453","sha1":"61c911a9b09dc7aaad720db67d3cb39394eaa132","sha256":"e68a5b19b365109e604c6a5b824e257ae04f72757cb8da52f3116add41f33c43"},"dob":"1979-07-06 04:55:11","registered":"2010-12-05 20:10:29","phone":"987-440-069","cell":"620-743-650","id":{"name":"DNI","value":"64190365-N"},"picture":{"large":"https://randomuser.me/api/portraits/women/72.jpg","medium":"https://randomuser.me/api/portraits/med/women/72.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/72.jpg"},"nat":"ES"}],"info":{"seed":"80aa51e344d0db3a","results":1,"page":1,"version":"1.1"}} 

如何去解析多陣列JSON,得到的名稱:第一個和最後

+0

你有沒有調試,並通過您的代碼加強,看看發生了什麼? –

+2

我會假設你需要創建完整的對象不只是一個部分 – TheLethalCoder

+1

'公共字符串results.name.first {得到;組; }'不會編譯。 – Jonesopolis

回答

0

您應該創建完整JSON模式:

public class Name 
{ 
    public string title { get; set; } 
    public string first { get; set; } 
    public string last { get; set; } 
} 

public class Location 
{ 
    public string street { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public int postcode { get; set; } 
} 

public class Login 
{ 
    public string username { get; set; } 
    public string password { get; set; } 
    public string salt { get; set; } 
    public string md5 { get; set; } 
    public string sha1 { get; set; } 
    public string sha256 { get; set; } 
} 

public class Id 
{ 
    public string name { get; set; } 
    public string value { get; set; } 
} 

public class Picture 
{ 
    public string large { get; set; } 
    public string medium { get; set; } 
    public string thumbnail { get; set; } 
} 

public class Result 
{ 
    public string gender { get; set; } 
    public Name name { get; set; } 
    public Location location { get; set; } 
    public string email { get; set; } 
    public Login login { get; set; } 
    public string dob { get; set; } 
    public string registered { get; set; } 
    public string phone { get; set; } 
    public string cell { get; set; } 
    public Id id { get; set; } 
    public Picture picture { get; set; } 
    public string nat { get; set; } 
} 

public class Info 
{ 
    public string seed { get; set; } 
    public int results { get; set; } 
    public int page { get; set; } 
    public string version { get; set; } 
} 

public class RootObject 
{ 
    public List<Result> results { get; set; } 
    public Info info { get; set; } 
} 

然後反序列化JSON:

 //read the info 
     using (StreamReader st = new StreamReader(res.GetResponseStream())) 
     { 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      var objText = st.ReadToEnd(); 
      TwitterObject tn = js.Deserialize<TwitterObject>(objText); 

      foreach (var item in tn.results) 
      { 
       Response.Write(item.name.first + item.name.last + "<br />"); 
      } 
     } 
0

可以生成從您的JSON響應類型類,嘗試http://json2csharp.com/

例如

var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var results = jsSerializer.Deserialize<RootObject>(objText); 

那麼你可以得到第一個結果名稱如下

objText.results[0].Name.name 

生成類,如下

public class Name 
{ 
    public string title { get; set; } 
    public string first { get; set; } 
    public string last { get; set; } 
} 

public class Location 
{ 
    public string street { get; set; } 
    public string city { get; set; } 
    public string state { get; set; } 
    public int postcode { get; set; } 
} 

public class Login 
{ 
    public string username { get; set; } 
    public string password { get; set; } 
    public string salt { get; set; } 
    public string md5 { get; set; } 
    public string sha1 { get; set; } 
    public string sha256 { get; set; } 
} 

public class Id 
{ 
    public string name { get; set; } 
    public string value { get; set; } 
} 

public class Picture 
{ 
    public string large { get; set; } 
    public string medium { get; set; } 
    public string thumbnail { get; set; } 
} 

public class Result 
{ 
    public string gender { get; set; } 
    public Name name { get; set; } 
    public Location location { get; set; } 
    public string email { get; set; } 
    public Login login { get; set; } 
    public string dob { get; set; } 
    public string registered { get; set; } 
    public string phone { get; set; } 
    public string cell { get; set; } 
    public Id id { get; set; } 
    public Picture picture { get; set; } 
    public string nat { get; set; } 
} 

public class Info 
{ 
    public string seed { get; set; } 
    public int results { get; set; } 
    public int page { get; set; } 
    public string version { get; set; } 
} 

public class RootObject 
{ 
    public List<Result> results { get; set; } 
    public Info info { get; set; } 

} 
0

我相信如果你正在使用System.Web.Script.Serialization.JavaScriptSerializer,你的類型,序列化必須是1:1映射到JSON。因此,像下面將反序列化:

class TwitterName 
{ 
    public string first { get; set; } 
    public string last { get; set; } 
} 

class TwitterUser 
{ 
    public TwitterName name { get; set; } 
} 

class TwitterUsers 
{ 
    public TwitterUser[] results { get; set; } 
} 

var jss = new JavaScriptSerializer(); 
var users = jss.Deserialize<TwitterUsers>(jsonString); 

如果這不是你正在尋找的解決方案,我相信你可以有超過系列化更多的控制使用其他庫類型(即Json.Net)。

你也動態地將其反序列化的對象圖:

dynamic users = jss.DeserializeObject(jsonString); 
string firstUserName = users["results"][0]["name"]["first"];