【问题标题】:How to Deserialize given Json string to a defined class如何将给定的 Json 字符串反序列化为定义的类
【发布时间】:2017-10-19 12:05:12
【问题描述】:

下面是我的 Json 字符串

Json 字符串

{
  "RestResponse": {
    "messages": [
      "Country found matching code [IN]."
    ],
    "result": {
      "name": "India",
      "alpha2_code": "IN",
      "alpha3_code": "IND"
    }
  }
}

我在 Xamarin 中创建了这些类,但没有将 Json 解析为对象,请指导。

public class Country
{
    [JsonProperty(PropertyName = "RestResponse")]
    public List<myRestResponse> RestResponse { get; set; }
}

public class myRestResponse
{
    [JsonProperty(PropertyName = "messages")]
    public List<string> messages { get; set; }
    [JsonProperty(PropertyName = "result")]
    public List<Result> result { get; set; }
}

public class Result
{
    [JsonProperty(PropertyName = "name")]
    public string name { get; set; }
    [JsonProperty(PropertyName = "alpha2_code")]
    public string alpha2_code { get; set; }
    [JsonProperty(PropertyName = "alpha3_code")]
    public string alpha3_code { get; set; }
}

我正在使用下面的代码进行反序列化

var content = await response.Content.ReadAsStringAsync();
Country  country = JsonConvert.DeserializeObject<Country>(content);

【问题讨论】:

  • RestResponse 不是初学者的集合,result 也不是。

标签: c# json xamarin json.net


【解决方案1】:

使用http://json2csharp.com/ 等工具有助于定义您的类。

这给出了结果

public class Result
{
    public string name { get; set; }
    public string alpha2_code { get; set; }
    public string alpha3_code { get; set; }
}

public class RestResponse
{
    public List<string> messages { get; set; }
    public Result result { get; set; }
}

public class Country
{
    public RestResponse RestResponse { get; set; }
}

所以你可以看到你的 Country 类(Root 对象)不应该有一个列表。

并且 RestResponse 应该只包含一个 Result 对象,而不是一个列表。

【讨论】:

    【解决方案2】:

    根据您的类结构,您的 JSON 格式不正确。 JSON 有两个问题。根据您尝试反序列化的类结构,property'RestResponse' 是一个数组,但在您的 JSON 中它不是。另一个是属性“结果”,它也是一个数组,但在您的 JSON 中它不是。根据您的 JSON 格式更新您的类结构,或者请尝试以下 JSON,

    {
      "RestResponse": [
        {
          "messages": [ "Country found matching code [IN]." ],
          "result": [
            {
              "name": "India",
              "alpha2_code": "IN",
              "alpha3_code": "IND"
            }
          ]
        }
      ]
    }

    如果你想更新你的班级结构,请复制下面的班级,

    public class Result
    {
    public string name { get; set; }
    public string alpha2_code { get; set; }
    public string alpha3_code { get; set; }
    }
    
    public class RestResponse
    {
    public List<string> messages { get; set; }
    public Result result { get; set; }
    }
    
    public class RootObject
    {
    public RestResponse RestResponse { get; set; }
    }
    

    【讨论】:

      【解决方案3】:

      您的类定义与响应数据不匹配。您可以使用一些在线工具轻松创建类定义。如果您使用的是 Visual Studio,那么您可以简单地使用 Edit 菜单中的 Paste Special 选项。请记住,在粘贴之前,您必须先复制响应字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        相关资源
        最近更新 更多