开发者

Convert JSON File to C# Object

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 Carb开发者_如何学运维ine 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.


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜