[英]How to deserialize json to custom type
I have the following 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",
},
...
and created classes: 并创建类:
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; }
}
of course, it crashes when I try to do it: 当然,当我尝试这样做时会崩溃:
_jsonParse = JsonConvert.DeserializeObject<JsonParse>(jsonParseString);
I try to create a converter: 我尝试创建一个转换器:
public class CoordinateConverter : CustomCreationConverter<Coordinate>
{
public override Coordinate Create(Type objectType)
{
return new Coordinate();
}
}
and 和
_jsonParse = JsonConvert.DeserializeObject<JsonParse>(jsonParseString, new CoordinateConverter());
it does not work. 这是行不通的。 How to declare and use a converter correct way?
如何声明和使用转换器正确的方式?
I made it to create the following converter: 我创建了以下转换器:
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();
}
}
I use this class for all my objects: 我将此类用于所有对象:
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);
}
}
To use it: 要使用它:
Coordinate co = new Coordinate(){ X=10,Y=20 }
string json = JsonConverter.ObjectToString(co);
Coordinate coParsed = (Coordinate)StringToObject(json);
And include 并包括
Namespace: System.Web.Script.Serialization
And add reference too 并添加参考
Assembly: System.Web.Extensions (in System.Web.Extensions.dll)
You can use this for any type of object in this example I used Coordinate because I could declare it simply. 在本例中,您可以将其用于任何类型的对象,因为我可以简单地声明它,所以使用了Coordinate。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.