2016-11-11 67 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"];