简体   繁体   中英

How do I parse a json object containing a array of arrays into a list of objects in C# using Newtonsoft.Json?

I'm using json quite rarely, so I'm sorry if I didn't get all the namings right.

I have a predefined json string which contains a large number of points (x, y; integer), these points are contained in "value". Each point is a list of 2 values in JSON.

Stripped down to the main content the json string looks like this:

{"values":[[3621,67],[445,117]],"more_key":"move_value","much_more_key":"much_move_value"}

What I go working is this:

public class RootObject
{
    public List<int[]> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}

using the following line to deserialize (and serialize later on):

_rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
...
JsonConvert.SerializeObject(_rootObject);

But I do not want a list of int arrays. I would like to get a list of (point) objects. What I would like to get working is something like this:

public class RootObject
{
    public List<MyPoint> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}


public class MyPoint
{
    public int x;
    public int y;
    // more member variables not from json
    public bool notFromJSON;
}

So far I didn't find a way to accomplish this.

Between Deserializing and Serializing would like to do some work with this list of MyPoint and need additional members in this class.

Any hints how to get to this would be appreciated.

Thank you Brian Rogers and Omar Abdel Bari for pointing me in the right direction.

I needed to write a JsonConverter (as descripted here: Serializing/deserializing arrays with mixed types using Json.NET ), than it worked right away.

public class RootObject
{
    public List<MyPoint> values { get; set; }
    public string more_key { get; set; }
    public string much_more_key { get; set; }
}

[JsonConverter(typeof(MyPointConverter))]
public class MyPoint
{
    public int X;
    public int Y;
    // more member variables not from json
    public bool notFromJSON;

    public MyPoint() 
    {
        X = -1;
        Y = -1;
    }

}


class MyPointConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return (objectType == typeof(MyPoint));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        JArray ja = JArray.Load(reader);
        MyPoint myPoint = new MyPoint();
        myPoint.X = (int)ja[0];
        myPoint.Y = (int)ja[1];
        return myPoint;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        JArray ja = new JArray();
        MyPoint myPoint = (MyPoint)value;
        ja.Add(myPoint.X);
        ja.Add(myPoint.Y);
        ja.WriteTo(writer);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM