-2

I'm new to JSON and I'm trying to convert JSON data to C# class, but I always get errors when converting to C# entity class. Can someone tell me how to properly convert the following JSON data to C# class? Thank you very much! orz

[
    {
        "place_id":121943890,
        "licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
        "osm_type":"way",
        "osm_id":195289214,
        "boundingbox":["28.0650511","28.0769594","112.9936342","113.0111091"],
        "lat":"28.07081905",
        "lon":"113.002623874342",
        "display_name":"Changsha",
        "class":"amenity",
        "type":"university",
        "importance":0.11100000000000002,
        "icon":"https://nominatim.openstreetmap.org/images/mapicons/education_university.p.20.png",
        "geojson":{
            "type":"Polygon",
            "coordinates":[
                [
                    [112.9936342,28.0740059],
                    [112.9957532,28.0699078],
                    [112.9968442,28.0691153],
                    [112.9970274,28.0689822],
                    [113.0003451,28.0669209],
                    [113.0012613,28.0650511],
                    [113.0111091,28.0676428],
                    [113.0096377,28.0717303],
                    [113.0084561,28.0746411],
                    [113.0074304,28.0769594],
                    [113.0015116,28.0756923],
                    [113.0011744,28.0756201],
                    [112.9936342,28.0740059]
                ]
            ]
        }
    }
]
sroand
  • 55
  • 5

2 Answers2

0

Maybe this?

public class Geojson
{
    public string type { get; set; }
    public List<List<List<double>>> coordinates { get; set; }
}

public class RootObject
{
    public int place_id { get; set; }
    public string licence { get; set; }
    public string osm_type { get; set; }
    public int osm_id { get; set; }
    public List<string> boundingbox { get; set; }
    public string lat { get; set; }
    public string lon { get; set; }
    public string display_name { get; set; }
    public string @class { get; set; }
    public string type { get; set; }
    public double importance { get; set; }
    public string icon { get; set; }
    public Geojson geojson { get; set; }
}

I used this tool to do it http://json2csharp.com/

Maks Shapovalov
  • 154
  • 1
  • 6
  • I just tried it, but the following error is still reported – sroand Dec 28 '19 at 09:35
  • Newtonsoft.Json.JsonSerializationException:“Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'OSMDataGet.RootObject' because the type requires a JSON object (e.g. {"name":"value"... Path '', line 1, position 1.” – sroand Dec 28 '19 at 09:36
0

You can use quicktype for this. Just paste your JSON in and it will produce c# equivalent data structures along with some sample code to serialize and deserialize using Json.NET.

You can change the class names and property names after the fact, but you should not change the JsonProperty attributes.

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var welcome = Welcome.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class Welcome
    {
        [JsonProperty("place_id")]
        public long PlaceId { get; set; }

        [JsonProperty("licence")]
        public string Licence { get; set; }

        [JsonProperty("osm_type")]
        public string OsmType { get; set; }

        [JsonProperty("osm_id")]
        public long OsmId { get; set; }

        [JsonProperty("boundingbox")]
        public string[] Boundingbox { get; set; }

        [JsonProperty("lat")]
        public string Lat { get; set; }

        [JsonProperty("lon")]
        public string Lon { get; set; }

        [JsonProperty("display_name")]
        public string DisplayName { get; set; }

        [JsonProperty("class")]
        public string Class { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("importance")]
        public double Importance { get; set; }

        [JsonProperty("icon")]
        public Uri Icon { get; set; }

        [JsonProperty("geojson")]
        public Geojson Geojson { get; set; }
    }

    public partial class Geojson
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("coordinates")]
        public double[][][] Coordinates { get; set; }
    }

    public partial class Welcome
    {
        public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this Welcome[] self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}
William Xifaras
  • 5,212
  • 2
  • 19
  • 21