本文介绍了将 JSON 文件转换为 C# 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来将 JSON 文件转换为 C# 对象.我一直在使用 Json.NET 库.JSON 文件格式如下:

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

我创建了如下类来映射 JSON:

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

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

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

已编辑顺便说一句,我已经使用字典尝试了以下方法,但仍然不好.错误:无法将 JSON 数组反序列化为类型 'JSONConvertTester.WGS84Coordinate'."

Editedbtw, 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>

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

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

这篇关于将 JSON 文件转换为 C# 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:34