将JSON文件转换为C#对象

时间:2011-10-11 04:02:46

标签: c# json object

我需要一些帮助才能将JSON文件转换为C#对象。我一直在使用Json.NET库。 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"}]}

我已经创建了以下类来映射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;} 
}

我有以下代码来反序列化JSON:

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

代码似乎不起作用。我想念什么吗?请帮忙。 非常感谢。 克里斯

编辑 顺便说一句,我通过使用词典尝试了以下,但仍然不好。错误:“无法将JSON数组反序列化为'JSONConvertTester.WGS84Coordinate'类型。”

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

请有人提出建议,非常感谢。

3 个答案:

答案 0 :(得分:0)

你的JSON看起来有点奇怪 - 我希望看到更像下面的东西,它应该反序列化到你的目标类型而没有任何问题。

[
   {
     "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"
   }
]

我使用以下代码成功测试了这个: -

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

如果您坚持使用您提供的格式使用JSON,那么您可能必须先将其转换为可导航的JSON文档,然后自行将转换写入目标类型。

答案 1 :(得分:0)

您可以尝试反序列化为类型Dictionary&gt;

我使用.net的内部序列化器/解串器,你可以试试这样的东西 -

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

答案 2 :(得分: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);

    }
}

<强>输出:

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