字符串和对象(数组)的JSON DeserializeObject错误

时间:2015-05-07 18:35:03

标签: json.net

这是我的JSON值

{ "spatialReference": 
    { "wkid": 4326, "latestWkid": 4326 }, 
 "candidates": 
  [ 
      { "address": "380 New York St, Redlands, California, 92373",   "location": { "x": -117.19564110200449, "y": 34.057084093752607 }, "score": 99.890000000000001, "attributes": { } }, 
      { "address": "380 New York St, Redlands, California, 92373", "location": { "x": -117.19564235064003, "y": 34.057118309276547 }, "score": 99.890000000000001, "attributes": { } }, 
      { "address": "381 New York St, Redlands, California, 92373", "location": { "x": -117.19570747666533, "y": 34.057117640108572 }, "score": 78.890000000000001, "attributes": { } }, 
      { "address": "New York St, Redlands, California, 92373", "location": { "x": -117.19564313410432, "y": 34.054959327808675 }, "score": 99.890000000000001, "attributes": { } }, 
      { "address": "92373, Redlands, California", "location": { "x": -117.18448413080158, "y": 34.042938592774277 }, "score": 100, "attributes": { } }, 
      { "address": "Redlands, California", "location": { "x": -117.18259971082223, "y": 34.055564407815503 }, "score": 98.780000000000001, "attributes": { } } 

  ]
  }

这是代码:

public class CanAttributes
    {
        public string StreetName { get; set; }
        public string StreetType { get; set; }
    }

    public class Candidates
    {
        [JsonProperty("address")]
        public string Address { get; set; }
        [JsonProperty("location")]
        public GeoLocation Location { get; set; }
        [JsonProperty("score")]
        public decimal Score { get; set; }
        [JsonProperty("attributes")]
        public CanAttributes Attributes { get; set; }
    }

    public class jsCandidates
    {
        [JsonProperty("spatialReference")]
        public string spatialReference { get; set; }  
         [JsonProperty("candidates")]
        public Candidates[] candidates { get; set; }
    }

    public class GeoLocation
    {
        [JsonProperty("x")]
        public decimal X { get; set; }

        [JsonProperty("y")]
        public decimal Y { get; set; }
    }



   jsCandidates canArray = JsonConvert.DeserializeObject<jsCandidates>(str);

它给了我“错误读取字符串。意外的令牌:StartObject。路径'spatialReference',第2行,位置23”当我尝试反序列化上面的json值时。  任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

不是字符串,你应该改为对象。

public string spatialReference { get; set; } 

或者改为:

public class SpatialReference
{
    public int wkid { get; set; }
    public int latestWkid { get; set; }
}