2017-05-14 68 views
0

我有以下的JSON:如何反序列化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()); 

这是行不通的。如何正确声明和使用转换器?

+1

你什么错误? – SLaks

+0

将值“2,8”转换为键入“Domain.Coordinate”时出错 –

回答

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) 

在这个例子中,你可以将它用于任何类型的对象我使用了坐标,因为我可以简单地声明它。

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