Json 字符串转换为列表 windows phone 8

Json string convert to List for windows phone 8

[  
   {  
      "TotalCount":5,
      "purchaseStatus":"0",
      "inviteCount":0,
      "status":"success",
      "statusCode":"200",
      "message":"success",
      "user":[  
         {  
            "id":"ac789045e3f109f536e8c217e4e78fe6",
            "firstname":"Srinivasan",
            "lastname":"CP",
            "emailid":"seenu4689@gmail.com",
            "prof_image_path":"http:\/\/gps-dev.gpsmobitrack.com\/pickzy_dev\/upload\/img_2015-04-11-09-04-43.png",
            "gcm_regid":"NULL",
            "ios_device_id":"",
            "longitude":"80.245255",
            "latitude":"13.035394",
            "relationship":"Friend",
            "track_userid":"361",
            "date":"2015-04-18",
            "time":"13:08"
         },
         {  
            "id":"7ca84ee8cc024e3cf2f0736f89dc4ece",
            "firstname":"srini",
            "lastname":"cp",
            "emailid":"srinipickzy3@gmail.com",
            "prof_image_path":"http:\/\/gps-dev.gpsmobitrack.com\/pickzy_dev\/upload\/img_2015-04-11-08-04-41.png",
            "gcm_regid":"",
            "ios_device_id":"",
            "longitude":"80.245255",
            "latitude":"13.035394",
            "relationship":"Friend",
            "track_userid":"365",
            "date":"2015-04-21",
            "time":"19:26"
         },
         {  
            "id":"ef5b4c49b893c55ec77428b2c265510e",
            "firstname":"Naveen",
            "lastname":"Kumar",
            "emailid":"naveen.kumar@pickzy.com",
            "prof_image_path":"http:\/\/gps-dev.gpsmobitrack.com\/pickzy_dev\/upload\/img_2015-04-11-08-04-51.png",
            "gcm_regid":"NULL",
            "ios_device_id":"fbe87350c14ccbccc1ec4ad90986faa9c1124273ed9fb7708965c83a06dd7c32",
            "longitude":"80.245392",
            "latitude":"13.035848",
            "relationship":"Brother",
            "track_userid":"367",
            "date":"2015-04-10",
            "time":"17:45"
         },
         {  
            "id":"e6050d8b5d378cbc791ebab1295dd301",
            "firstname":"Parthiban",
            "lastname":"V M",
            "emailid":"parthibanemi@gmail.com",
            "prof_image_path":"http:\/\/gps-dev.gpsmobitrack.com\/pickzy_dev\/upload\/img_2015-03-28-07-03-52.png",
            "gcm_regid":"NULL",
            "ios_device_id":"",
            "longitude":"0.000000",
            "latitude":"0.000000",
            "relationship":"Friend",
            "track_userid":"368",
            "date":"2015-04-21",
            "time":"01:13"
         },
         {  
            "id":"9848d03341103f52816bf4d5a1e5467c",
            "firstname":"Naveen kumar",
            "lastname":"kumar",
            "emailid":"naveenkumar3506@gmail.com",
            "prof_image_path":"http:\/\/gps-dev.gpsmobitrack.com\/pickzy_dev\/upload\/img_2015-04-11-02-04-01.png",
            "gcm_regid":"NULL",
            "ios_device_id":"da8ef3258c4fbc42085ab1dcb62c7b6a4bd60112bb5507284a7a8b3958808c43",
            "longitude":"80.244835",
            "latitude":"13.035032",
            "relationship":"Other",
            "track_userid":"370",
            "date":"2015-04-18",
            "time":"19:38"
         }
      ]
   }
]

首先创建你的数据类,你可以使用json2csharp to create them for you. Then use NuGet to add a reference Json.Net

然后你需要这样的东西:

JsonConvert.DeserializeObject<RootObject>(YourJson)

一些供您研究的方向。

3 步

  • 获取 json 作为字符串
  • 将json解析为一个对象
  • 将对象绑定到您的列表

获取 json 作为字符串

将您的 json 放入字符串中。你必须下载它吗?找这个。

解析json到对象

构建对象架构。您的 json 中包含哪些内容?您可以使用 Visual Studio 来帮助我们使用 json2csharp 这样的工具。现在,你必须反序列化。 您可以使用像 Json.NET 这样的库来为您完成这项工作。我让你看看。

将对象绑定到您的列表

为了保险起见,您需要一个列表框?

使用 Json2Csharp 网站将您的 json 字符串转换为模型 class。

您的 Json 数据的 classes 模型将是这样的:

public class User
{
    public string id { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string emailid { get; set; }
    public string prof_image_path { get; set; }
    public string gcm_regid { get; set; }
    public string ios_device_id { get; set; }
    public string longitude { get; set; }
    public string latitude { get; set; }
    public string relationship { get; set; }
    public string track_userid { get; set; }
    public string date { get; set; }
    public string time { get; set; }
}

public class RootObject
{
    public int TotalCount { get; set; }
    public string purchaseStatus { get; set; }
    public int inviteCount { get; set; }
    public string status { get; set; }
    public string statusCode { get; set; }
    public string message { get; set; }
    public List<User> user { get; set; }
}

在那之后,正如@hans Olsson 所说,使用 Json.Net Nu-Get 包库将 json 字符串反序列化为模型 classes 那些你刚从 Json 得到的2Csharp 网站。

JsonConvert.DeserializeObject<RootObject>("") // your json data as string in the parameter.