Jenny
Jenny

Reputation: 15

JSON DeserializeObject error for a string and an object (array)

This is my JSON value

{ "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": { } } 

  ]
  }

This is code:

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);

It gives me "Error reading string. Unexpected token: StartObject. Path 'spatialReference', line 2, position 23" when I tried to deserialized the above json value. Any help would be appreciated.

Upvotes: 0

Views: 216

Answers (1)

Nguyen Kien
Nguyen Kien

Reputation: 1927

Not string, you should change to object.

public string spatialReference { get; set; } 

Or this instead:

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

Upvotes: 1

Related Questions