【问题标题】:Json Deserialization | Cannot Deserialize Current JsonJson 反序列化 |无法反序列化当前 Json
【发布时间】:2021-07-21 01:00:15
【问题描述】:

我有下面的 Json -

{"property_id":"53863730","name":"Hayat Elhamra","address":{"line_1":"Jeddah","city":"Jeddah","state_province_name":"Jeddah","postal_code":"23212","country_code":"SA","obfuscation_required":false,"localized":{"links":{"ar-SA":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=ar-SA&property_id=53863730&include=address"}}}},"location":{"coordinates":{"latitude":21.520902,"longitude":39.158265}},"phone":"20-01145772035","category":{"id":"16","name":"Apartment"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":true},"dates":{"added":"2020-06-10T23:03:21.345Z","updated":"2020-06-10T23:03:23.701Z"},"chain":{"id":"0","name":"Independent"},"brand":{"id":"0","name":"Independent"}}
{"property_id":"53183065","name":"Carefully Furnished Bungalow With 2 Bathrooms, 7km From Pula","address":{"line_1":"1 x M 90,3","line_2":"PRIVATE_VACATION_HOME 3","city":"Fazana","state_province_name":"Istria (county)","postal_code":"52212","country_code":"HR","obfuscation_required":true,"localized":{"links":{"hr-HR":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=hr-HR&property_id=53183065&include=address"}}}},"ratings":{"property":{"rating":"3.0","type":"Star"}},"location":{"coordinates":{"latitude":44.93,"longitude":13.8}},"phone":"410442743080","category":{"id":"17","name":"Private vacation home"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":false},"dates":{"added":"2020-05-13T21:06:42.861Z","updated":"2020-05-18T21:57:39.242Z"},"statistics":{"1073743378":{"id":"1073743378","name":"Number of bedrooms - 2","value":"2"},"1073743380":{"id":"1073743380","name":"Max occupancy - 4","value":"4"},"1073743379":{"id":"1073743379","name":"Number of bathrooms - 2","value":"2"}},"chain":{"id":"7278","name":"Belvilla"},"brand":{"id":"7353","name":"Belvilla"}}
{"property_id":"53182898","name":"Snug Cottage in Pašman With Roofed Terrace","address":{"line_1":"Pasman","city":"Pasman","state_province_name":"Zadar","postal_code":"23260","country_code":"HR","obfuscation_required":true,"localized":{"links":{"hr-HR":{"method":"GET","href":"https://api.ean.com/2.4/properties/content?language=hr-HR&property_id=53182898&include=address"}}}},"ratings":{"property":{"rating":"1.0","type":"Star"}},"location":{"coordinates":{"latitude":43.891571,"longitude":15.423619}},"phone":"410442743080","category":{"id":"11","name":"Cottage"},"rank":99999999,"business_model":{"expedia_collect":true,"property_collect":false},"dates":{"added":"2020-05-13T21:13:49.155Z","updated":"2020-05-27T21:02:31.808Z"},"statistics":{"1073743378":{"id":"1073743378","name":"Number of bedrooms - 2","value":"2"},"1073743380":{"id":"1073743380","name":"Max occupancy - 5","value":"5"},"1073743379":{"id":"1073743379","name":"Number of bathrooms - 1","value":"1"}},"chain":{"id":"7278","name":"Belvilla"},"brand":{"id":"7353","name":"Belvilla"}}

为此,我创建了下面的类结构 -

public class Property
{
  public string property_id { get; set; }
  public string name { get; set; }
  public Address address { get; set; }
  public Location location { get; set; }
  public string phone { get; set; }
  public Category category { get; set; }
  public int rank { get; set; }
  public Business_Model business_model { get; set; }
  public Dates dates { get; set; }
  public Chain chain { get; set; }
  public Brand brand { get; set; }
}

public class Address
{
  public string line_1 { get; set; }
  public string city { get; set; }
  public string state_province_name { get; set; }
  public string postal_code { get; set; }
  public string country_code { get; set; }
  public bool obfuscation_required { get; set; }
  public Localized localized { get; set; }
}

public class Localized
{
  public Links links { get; set; }
}

public class Links
{
  public ArSA arSA { get; set; }
}

public class ArSA
{
  public string method { get; set; }
  public string href { get; set; }
}

public class Location
{
  public Coordinates coordinates { get; set; }
}

public class Coordinates
{
  public float latitude { get; set; }
  public float longitude { get; set; }
}

public class Category
{
  public string id { get; set; }
  public string name { get; set; }
}

public class Business_Model
{
  public bool expedia_collect { get; set; }
  public bool property_collect { get; set; }
}

public class Dates
{
  public DateTime added { get; set; }
  public DateTime updated { get; set; }
}

public class Chain
{
  public string id { get; set; }
  public string name { get; set; }
}

public class Brand
{
  public string id { get; set; }
  public string name { get; set; }
}

我在下面的代码中出现错误 -

using (StreamReader streamReader = new StreamReader("d://propertycontent.expediacollect.en-US.json"))
      {
        using (var json = new JsonTextReader(streamReader))
        {
          JsonSerializer serializer = new JsonSerializer();
          var properties= (List<Property>)serializer.Deserialize(json, typeof(List<Property>));       
        }       
      }

错误 -

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Property]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'property_id', line 1, position 15.'

【问题讨论】:

  • 问题中的格式,它看起来不像有效的 json。尝试将 {[ 添加到 json 的开头,将 ]} 添加到末尾。这将使它成为List&lt;Property&gt;
  • 是三个单独的字符串还是一个字符串?旁注:您可以使用通用 serializer.Deserialize&lt;List&lt;Property&gt;&gt; 并节省投射

标签: c# json .net


【解决方案1】:

它没有反序列化,因为它不是有效的 json。要使其有效,并使其成为List&lt;Property&gt;,请将[ 添加到json 的开头,将] 添加到json 的末尾。只需将 json 包含在 [ ... ] 中以使其有效,并且假设其余部分有效并且没有丢失任何逗号或括号,它将反序列化。

【讨论】:

  • 它应该是{[ ]},因为无论它是列表/数组,整个json都需要包含在{ }中。在这种情况下,[ ] 用于List&lt;Property&gt;。 Json 不管它包含什么都需要在{ } 中。如果它还没有在他的 json 中很难分辨,那么他们将需要添加它。这就是为什么它需要一个额外的{ },所以总共需要添加一个{[ ]}
  • 我看到了小提琴,我明白了。如果您认为我错了,请尝试在 json 中放置一个对象。而不是字符串列表尝试放置一个具有属性的类实例,如果它不适用于{},我会改变我的答案。
  • dotnetfiddle.net/jEtilt 这里有一个简单的变化。在序列化和反序列化类实例时,括号是必需的。
  • 我的立场是正确的。 dotnetfiddle.net/GUV5Ef我的错。你说的对。我会改变我的答案。
【解决方案2】:

试试这个,你必须安装 NewtonsoftJson。它使用 Visual Studio 和 Postman 进行了测试,并且可以正常工作。

var jsonOrig= ...your json
var json = JsonConvert.SerializeObject(jsonOrig);
var jsonObj = JsonConvert.DeserializeObject<DataRoot>(json);

public class DataRoot
    {
        public string property_id { get; set; }
        public string name { get; set; }
        public Address address { get; set; }
        public Location location { get; set; }
        public string phone { get; set; }
        public Category category { get; set; }
        public int rank { get; set; }
        public BusinessModel business_model { get; set; }
        public Dates dates { get; set; }
        public Chain chain { get; set; }
        public Brand brand { get; set; }
    }
public class ArSA
    {
        public string method { get; set; }
        public string href { get; set; }
    }

    public class Links
    {
        [JsonProperty("ar-SA")]
        public ArSA ArSA { get; set; }
    }

    public class Localized
    {
        public Links links { get; set; }
    }

    public class Address
    {
        public string line_1 { get; set; }
        public string city { get; set; }
        public string state_province_name { get; set; }
        public string postal_code { get; set; }
        public string country_code { get; set; }
        public bool obfuscation_required { get; set; }
        public Localized localized { get; set; }
    }

    public class Coordinates
    {
        public double latitude { get; set; }
        public double longitude { get; set; }
    }

    public class Location
    {
        public Coordinates coordinates { get; set; }
    }

    public class Category
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class BusinessModel
    {
        public bool expedia_collect { get; set; }
        public bool property_collect { get; set; }
    }

    public class Dates
    {
        public DateTime added { get; set; }
        public DateTime updated { get; set; }
    }

    public class Chain
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    public class Brand
    {
        public string id { get; set; }
        public string name { get; set; }
    }

    `````

【讨论】:

  • 考虑到问题中的实际问题没有得到解决,这很有趣。
猜你喜欢
  • 2021-02-11
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
相关资源
最近更新 更多