阅读Json Serialized c#

时间:2016-11-26 02:17:39

标签: c# json mvvm

我试图从这个json文本中获取id。我不确定如何获取它并不能让我创建一个json对象。我尝试反序列化它:

public async Task<List<T>> GetIdAsync(string username)
{
    var httpClient = new HttpClient();

    var json = await httpClient.GetStringAsync(WebServiceURL);

    var  Id  = JsonConvert.DeserializeObject<List<T>>(json);

   return Id;
}

但是我在反序列化期间遇到了这个错误

  

未处理的例外:

     

Newtonsoft.Json.JsonReaderException:遇到意外的字符   在解析值时:{。路径&#39;&#39;,第1行,第2位

这是json文本:

json    "[{\"Id\":1,\"FirstName\":\"a\",\"LastName\":\"b\",\"UserName\":\"user\",\"Email\":\"d@gmail.com\",\"Password\":\"poSsUrscOuXL+E5qxXsCsA==\",\"CompanyName\":\"c\",\"Salt\":\"n3ZLSwU9L1g=\",\"Address\":{\"Country\":\"c\",\"Province\":\"p\",\"City\":\"c\",\"PostalCode\":\"p\",\"Street\":\"street\",\"StreetNumber\":\"123\"},\"Telephone\":{\"PersonalPhoneNumber\":\"413414\",\"BusinessPhoneNumber\":\"134134\"},\"Location\":{\"Lat\":45.6025839,\"Lng\":-97.8489959}}]"   string

2 个答案:

答案 0 :(得分:1)

给定JSON的值,它是一个对象数组。您可以使用动态对象仅提取所需的属性。首先将json反序列化为List<dynamic>,获取列表中的第一个对象并调用Id属性。

public async Task<int> GetIdAsync(string username) {
    var httpClient = new HttpClient();

    var json = await httpClient.GetStringAsync(WebServiceURL);

    var list  = JsonConvert.DeserializeObject<List<dynamic>>(json);

    var obj = list.FirstOrDefault();

    int Id = obj != null ? obj.Id : 0;

    return Id;
}

更新:

在简单的单元测试中测试并按预期传递。

[TestClass]
public class UnitTest6 {
    [TestMethod]
    public async Task GetIdAsyncTest() {

        var id = await GetIdAsync("");

        Assert.IsTrue(id > o);
    }

    public async Task<int> GetIdAsync(string username) {
        var httpClient = new HttpClient();
        //simple data just for the purpose of the test.
        var json = await Task.FromResult("[{\"Id\":1,\"FirstName\":\"a\"}]"); //await httpClient.GetStringAsync("");

        var list = JsonConvert.DeserializeObject<List<dynamic>>(json);

        var obj = list.FirstOrDefault();

        int Id = obj != null ? obj.Id : 0;

        return Id;
    }
}

答案 1 :(得分:0)

您应该使用http://json2csharp.com/

创建Json文件的模型
public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string CompanyName { get; set; }
    public string Salt { get; set; }
    public Address Address { get; set; }
    public Telephone Telephone { get; set; }
    public Location Location { get; set; }
}

public class Address
{
    public string Country { get; set; }
    public string Province { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
    public string Street { get; set; }
    public string StreetNumber { get; set; }
}

public class Telephone
{
    public string PersonalPhoneNumber { get; set; }
    public string BusinessPhoneNumber { get; set; }
}

public class Location
{
    public float Lat { get; set; }
    public float Lng { get; set; }
}

然后将其反序列化:

public async Task<int> GetIdAsync(string username)
    {
        var httpClient = new HttpClient();

        var json = await httpClient.GetStringAsync(WebServiceURL);

        var  Id  = JsonConvert.DeserializeObject<Rootobject>(json).Property1.FirstOrDefault().Id;

       return Id;
};

<小时/> @JayPhillips希望拥有&#34;真实&#34;之后更新json文件:

[
   {
      "Id":1,
      "FirstName":"a",
      "LastName":"b",
      "UserName":"user",
      "Email":"d@gmail.com",
      "Password":"poSsUrscOuXL+E5qxXsCsA==",
      "CompanyName":"c",
      "Salt":"n3ZLSwU9L1g=",
      "Address":{
         "Country":"c",
         "Province":"p",
         "City":"c",
         "PostalCode":"p",
         "Street":"street",
         "StreetNumber":"123"
      },
      "Telephone":{
         "PersonalPhoneNumber":"413414",
         "BusinessPhoneNumber":"134134"
      },
      "Location":{
         "Lat":45.6025839,
         "Lng":-97.8489959
      }
   }
]