0

I need some help with converting JSON file to C# object. I've been using Json.NET library. JSON file format are as below:

{"174.845620 -36.913447 WGS84":[{"uuid":"a7e72b5c1fb96f1452d3c64fe89c7e6a","name":"35 Carbine Road","suburb":"Mount Wellington","town":"Auckland","district":"Auckland City","region":"Auckland","island":"North Island","x":2674839,"y":6474828,"longitude":174.845707,"latitude":-36.913385,"locality":"Mount Wellington, Auckland, Auckland City"}],"174.698503 -36.788258 WGS84":[{"uuid":"96fb8ae43b6791f5f2b7006d8818b9ad","name":"1\/248 Beach Haven Road","suburb":"Birkdale","town":"North Shore","district":"North Shore City","region":"Auckland","island":"North Island","x":2661988,"y":6488992,"longitude":174.698375,"latitude":-36.78816,"locality":"Birkdale, North Shore, North Shore City"}]}

I've created the following classes like this to map the JSON:

 public class WGS84Coordinate
{
    public string uuid{get; set;}
    public string name{ get; set;}
    public string suburb { get; set;}
    public string town { get; set;}
    public string district { get; set;}
    public string region { get; set;}
    public string island { get; set;}
    public int x { get; set;}
    public int y { get; set;}
    public double longitude { get; set;}
    public double latitude { get; set;}
    public string locality { get; set;}
}

    public class WGS84Coordinates
{
    public WGS84Coordinate wgs84Coodinate{ get; set;} 
}

and I have the following code to deserialize the JSON:

   List<WGS84Coordinates> r = JsonConvert.DeserializeObject<List<WGS84Coordinates>>(json);
                    if (r.Count > 0)
                    {
                        json = r[0].wgs84Coodinate.uuid + r[0].wgs84Coodinate.suburb;
                    }

The code doesn't seem to be working. Do I miss anything? Please do help. Many thanks. Chris

Edited btw, I've tried the following by using Dictionary and still not good. ERROR: "Cannot deserialize JSON array into type 'JSONConvertTester.WGS84Coordinate'."

    Dictionary<string, WGS84Coordinate> r = JsonConvert.DeserializeObject<Dictionary<string, WGS84Coordinate>>(json); // deserialize
    foreach (KeyValuePair<string, WGS84Coordinate> o in r)
    {
        json = o.Value.uuid;
    }

Please anyone kindly advise, Many thanks.

3
  • I use JavaScriptSerializer this works fine for me,check out: stackoverflow.com/questions/7699972/… Commented Oct 11, 2011 at 4:25
  • 1
    what happens? do you get an exception? if so, please include it in your question Commented Oct 11, 2011 at 5:05
  • I keep getting the following error: "Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[JSONConvertTester.WGS84Coordinates]'." Commented Oct 11, 2011 at 9:39

3 Answers 3

0

You may try deserialising into type Dictionary>

I use .net's internal serialiser/deserialiser, you can try something like this -

public Dictionary<string, List<WGS84Coordinate>> DesrialiseForMe(string jsonString)
{
    JavaScriptSerializer _s = new JavaScriptSerializer();
    Dictionary<string, List<WGS84Coordinate>> my_object = _s.Deserialise<Dictionary<string, List<WGS84Coordinate>>>(jsonString);
    return my_object;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi saarthak,Thank you for your post. Actually it works for my case. Now I can see the JSON object being deserialized into my target types. Thanks again, You've saved my day.
0

Your JSON looks a little strange - I would expect to see something more like the following, which should deserialize into your target types without any issue.

[
   {
     "uuid":"a7e72b5c1fb96f1452d3c64fe89c7e6a",
     "name":"35 Carbine Road",
     "suburb":"Mount Wellington",
     "town":"Auckland",
     "district":"Auckland City",
     "region":"Auckland",
     "island":"North Island",
     "x":2674839,
     "y":6474828,
     "longitude":174.845707,
     "latitude":-36.913385,
     "locality":"Mount Wellington, Auckland, Auckland City"
   },
   {
     "uuid":"96fb8ae43b6791f5f2b7006d8818b9ad",
     "name":"1\/248 Beach Haven Road",
     "suburb":"Birkdale",
     "town":"North Shore",
     "district":"North Shore City",
     "region":"Auckland",
     "island":"North Island",
     "x":2661988,
     "y":6488992,
     "longitude":174.698375,
     "latitude":-36.78816,
     "locality":"Birkdale, North Shore, North Shore City"
   }
]

I tested this succcessfully with the following code:-

// referencing System.Web.Extensions.dll
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
var list = ser.Deserialize<List<WGS84Coordinate>>(json);

If you are stuck with working with the JSON in the format you have given, then you will probably have to convert it to a navigable JSON document first and then write the conversion to your target types yourself.

1 Comment

Hi Adam, Thanks very much for your answer. that's the default format from the API of map provider. Any other ways to read this strange format as I dont really want to fiddle the return format from them? Or I can only do what you suggest to reformat it before converting to my target types? Cheers,
0
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class WGS84Coordinate {
    public string uuid{get; set;}
    public string name{ get; set;}
    public string suburb { get; set;}
    public string town { get; set;}
    public string district { get; set;}
    public string region { get; set;}
    public string island { get; set;}
    public int x { get; set;}
    public int y { get; set;}
    public double longitude { get; set;}
    public double latitude { get; set;}
    public string locality { get; set;}
}

public class WGS84Coordinates{
    public string tag { get; set; }
    public WGS84Coordinate wgs84Coodinate{ get; set;} 
}

class Sample {
    static public void Main(){
        const string json =
        @"{""174.845620 -36.913447 WGS84"":[{""uuid"":""a7e72b5c1fb96f1452d3c64fe89c7e6a"",""name"":""35 Carbine Road"",""suburb"":""Mount Wellington"",""town"":""Auckland"",""district"":""Auckland City"",""region"":""Auckland"",""island"":""North Island"",""x"":2674839,""y"":6474828,""longitude"":174.845707,""latitude"":-36.913385,""locality"":""Mount Wellington, Auckland, Auckland City""}],""174.698503 -36.788258 WGS84"":[{""uuid"":""96fb8ae43b6791f5f2b7006d8818b9ad"",""name"":""1\/248 Beach Haven Road"",""suburb"":""Birkdale"",""town"":""North Shore"",""district"":""North Shore City"",""region"":""Auckland"",""island"":""North Island"",""x"":2661988,""y"":6488992,""longitude"":174.698375,""latitude"":-36.78816,""locality"":""Birkdale, North Shore, North Shore City""}]}";

        dynamic json_obj =  JsonConvert.DeserializeObject(json);
        var datas = new List<WGS84Coordinates>();
        foreach(dynamic x in json_obj){
            dynamic o = x.Value[0];
            WGS84Coordinate w = new WGS84Coordinate {
                uuid      = o.uuid,
                name      = o.name,
                suburb    = o.suburb,
                town      = o.town,
                district  = o.district,
                region    = o.region,
                island    = o.island,
                x         = o.x,
                y         = o.y,
                longitude = o.longitude,
                latitude  = o.latitude,
                locality  = o.locality
            };
            datas.Add(new WGS84Coordinates {tag = x.Name, wgs84Coodinate = w });
        }
        //check
        Console.WriteLine("{0} recodes.", datas.Count);
        Console.WriteLine("tag:{0},name:{1}", datas[0].tag,datas[0].wgs84Coodinate.name);

    }
}

OUTPUT:

2 recodes.
tag:174.845620 -36.913447 WGS84,name:35 Carbine Road

1 Comment

Hi BLUEPIXY, Thanks very much for your answer. I've tried your suggestion, however, I am coding in .NET 2.0, not 4.0. Sorry didnt say that in the article. Could you please modify the code to cater for 2.0 framework please? Many thanks.

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.