0

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.

2
  • It seems like you have declared wrong properties names in class GeoLocation. You declared them as Lat and Long while in your JSON they are x and y. Also, i would recommend you to use JSON 2 C#: json2csharp.com Commented May 7, 2015 at 19:29
  • I edited the code to make it's more clear, but the issue still exist. Commented May 7, 2015 at 20:08

1 Answer 1

1

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.