0

I've below JSON which I want to convert into C# classes so that I can manipulate values in code once loaded. Below is JSON that I'm trying to parse:

    {
    "objects": {
        "gujarat": {
            "type": "GeometryCollection",
            "geometries": [{
                    "arcs": [
                        [0, 1],
                        [2]
                    ],
                    "type": "Polygon",
                    "properties": {
                        "dt_code": "491",                           
                        "year": "2011_c"
                    }
                },
                {
                    "arcs": [
                        [
                            [3]
                        ],
                        [
                            [4]
                        ],
                        [
                            [5]
                        ],
                        [
                            [6]
                        ],
                        [
                            [7, 8]
                        ],
                        [
                            [9]
                        ],
                        [
                            [10, 11, 12, 13, 14, 15, 16]
                        ]
                    ],
                    "type": "MultiPolygon",
                    "properties": {
                        "dt_code": "480",
                        "year": "2011_c"
                    }
                }
            ]
        }
    }
}

The real challenge I'm having is with the "arcs" property. If you look at the values for arcs property, it has two dimensional value in first object whereas three dimensional in second object..

Below are some examples that I've tried so far but they don't work:

//This doesn't load value for second object
public class Geometry
{
    public int [][] arcs { get; set; }
    public string type { get; set; }
    public Dictionary<string, string> properties { get; set; }
}

//This fails in deserialization
public class Geometry
{
    public int [][][] arcs { get; set; }
    public string type { get; set; }
    public Dictionary<string, string> properties { get; set; }
}

//This doesn't return value of arcs when it's serialized again and instead returns blank arrays.
public class Geometry
{
    public dynamic arcs { get; set; }
    public string type { get; set; }
    public Dictionary<string, string> properties { get; set; }
}

I want to load this JSON object, manipulate some values and then serialize again to send it to requester.

Hiren Desai
  • 941
  • 1
  • 9
  • 33

2 Answers2

0

Try this structure:

public partial class MainObjClass
    {
        [JsonProperty("objects")]
        public Objects Objects { get; set; }
    }

    public partial class Objects
    {
        [JsonProperty("gujarat")]
        public Gujarat Gujarat { get; set; }
    }

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

        [JsonProperty("geometries")]
        public Geometry[] Geometries { get; set; }
    }

    public partial class Geometry
    {
        [JsonProperty("arcs")]
        public Arc[][] Arcs { get; set; }

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

        [JsonProperty("properties")]
        public Properties Properties { get; set; }
    }

    public partial class Properties
    {
        [JsonProperty("dt_code")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long DtCode { get; set; }

        [JsonProperty("year")]
        public string Year { get; set; }
    }

    public partial struct Arc
    {
        public long? Integer;
        public long[] IntegerArray;

        public static implicit operator Arc(long Integer) => new Arc { Integer = Integer };
        public static implicit operator Arc(long[] IntegerArray) => new Arc { IntegerArray = IntegerArray };
    }

You have to break down each key element from JSON to a C# class/structure. Then nest objects and parse.

0

You can use json to c# converters available online to do so. below online tools can you help you generate c# code:

the second one quicktype has extension that you can install on visual studio and convert json on the fly to cs class.

But sometimes the generated class file from these extension is somewhat not fitting what we require and get more control of what information is to be accepted in the class. Like process the setter values. So for this i recommend going old school way for more control.

Side note : Also before you try to parse json in any form, if you are manually creating the json input then you need to check if the json is valid, because misplaced curly braces can be a huge pain, telling from my experience. So just check the json validity online using such tool

MDT
  • 1,535
  • 3
  • 16
  • 29