如何将json反序列化为自定义类型

时间:2017-05-14 13:37:52

标签: c# json json.net

我有以下json:

{
  "ExitCode": 1,
  "ErrorMessage": "",
  "NumberOfMatches": 9,
  "NumberOfExtractFrames": 3,
  "ProcessingTime": 111,
  "MatchResult": [
    {
      "TopLeft": "2, 8",
      "BottomRight": "377, 157",
      "Confidence": 1.0,
      "HighConfidenceLevel": true,
      "SearchFrame": "77, 69, 36, 26",
    },
    {
      "TopLeft": "2, 169",
      "BottomRight": "377, 318",
      "Confidence": 0.99999982118606567,
      "HighConfidenceLevel": true,
      "SearchFrame": "77, 230, 36, 26",
    },
...

并创建了类:

public class JsonParse
{
    public int ExitCode { get; set; }
    public string ErrorMessage { get; set; }
    public int NumberOfMatches { get; set; }
    public int NumberOfExtractFrames { get; set; }
    public int ProcessingTime { get; set; }

    public List<MatchResult> MatchResult { get; set; }

}

public class MatchResult
{
    public Coordinate TopLeft { get; set; }
    public Coordinate BottomRight { get; set; }
    public decimal Confidence { get; set; }
    public bool HighConfidenceLevel { get; set; }
    //public Tuple<int, int, int, int> SearchFrame { get; set; }
}

public class Coordinate
{
    public int X { get; set; }
    public int Y { get; set; }
}

当然,当我尝试这样做时它会崩溃:

_jsonParse = JsonConvert.DeserializeObject<JsonParse>(jsonParseString);

我尝试创建转换器:

public class CoordinateConverter : CustomCreationConverter<Coordinate>
{
    public override Coordinate Create(Type objectType)
    {
        return new Coordinate();
    }
}

_jsonParse = JsonConvert.DeserializeObject<JsonParse>(jsonParseString, new CoordinateConverter());

它不起作用。如何以正确的方式声明和使用转换器?

2 个答案:

答案 0 :(得分:2)

我创建了以下转换器:

public class CoordinateConverter : CustomCreationConverter<Coordinate>
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var values = reader.Value.ToString().Split(',').Select(n => Convert.ToInt32(n)).ToArray();
        Coordinate coordinates = new Coordinate() { X = values[0], Y = values[1] };
        return coordinates;
    }

    public override bool CanConvert(Type objectType)
    {
        if (objectType == typeof(Coordinate))
        {
            return true;
        }

        return false;
    }

    public override Coordinate Create(Type objectType)
    {
        return new Coordinate();
    }
}

答案 1 :(得分:0)

我将这个类用于所有对象:

public class JsonConverter
{
    public static string ObjectToString(object o)
    {
        var javaScriptSerializer = new JavaScriptSerializer();
        string jsonString = javaScriptSerializer.Serialize(o);

        return jsonString;
    }

    public static object StringToObject(string data)
    {
        var json_serializer = new JavaScriptSerializer();
        return json_serializer.DeserializeObject(data);
    }
}

使用它:

Coordinate co = new Coordinate(){ X=10,Y=20 }
string json = JsonConverter.ObjectToString(co);
Coordinate coParsed = (Coordinate)StringToObject(json);

并包括

Namespace:   System.Web.Script.Serialization

并添加参考

Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

在本例中我可以将它用于任何类型的对象我使用了Coordinate,因为我可以简单地声明它。