1 [JsonObject(MemberSerialization.OptOut)] 2 class FromPythonObject 3 { 4 [JsonProperty("has_caption")] 5 public bool _HasCatpion; 6 7 [JsonProperty("word_area_list")] 8 public List<Word> _WordList; 9 } 10 11 [JsonObject(MemberSerialization.OptOut)] 12 public class Word 13 { 14 [JsonProperty("upper_border")] 15 public int _UpperBorder; 16 17 [JsonProperty("bottom_border")] 18 public int _BottomBorder; 19 20 [JsonProperty("right_border")] 21 public int _RightBorder; 22 23 [JsonProperty("left_border")] 24 public int _LeftBorder; 25 26 [JsonProperty("content")] 27 public string _Content; 28 29 }
FromPythonObject中含有属性List<**Word**>
反序列化:
FromPythonObject word_list = JsonConvert.DeserializeObject<FromPythonObject>(jsonString);
报错:Error converting value "{"right_border": 502, "left_border": 455, "upper_border": 9, "bottom_border": 31, "content": "and"}" to type 'RecognitionModule.Model.Word'. Path 'word_area_list[0]', line 1, position 154.
将"{"right_border": 502, "left_border": 455, "upper_border": 9, "bottom_border": 31, "content": "and"}" 直接反序列化Word对象没有报错
Word word = JsonConvert.DeserializeObject<Word>("{\"right_border\": 502, \"content\": \"and\", \"upper_border\": 9, \"left_border\": 455, \"bottom_border\": 31}");
请问为什么序列化FromPythonObject对象会报错呢?
我的json字符串是
"{\"word_area_list\": [\"{\\\"left_border\\\": 455, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"and\\\", \\\"right_border\\\": 502}\", \"{\\\"left_border\\\": 516, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"make\\\", \\\"right_border\\\": 585}\", \"{\\\"left_border\\\": 597, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"the\\\", \\\"right_border\\\": 638}\", \"{\\\"left_border\\\": 651, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"land\\\", \\\"right_border\\\": 704}\", \"{\\\"left_border\\\": 718, \\\"upper_border\\\": 8, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"infertile\\\", \\\"right_border\\\": 817}\"], \"has_caption\": true}"
是因为[""]中的双引号,和\\\"应该是\"
数组和对象的特性不一样吧.[JsonProperty("word_area_list")]
你看下是不是这个问题.
谢谢你的回答,但是不是这个原因
我的json字符串是
"{\"word_area_list\": [\"{\\\"left_border\\\": 455, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"and\\\", \\\"right_border\\\": 502}\", \"{\\\"left_border\\\": 516, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"make\\\", \\\"right_border\\\": 585}\", \"{\\\"left_border\\\": 597, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"the\\\", \\\"right_border\\\": 638}\", \"{\\\"left_border\\\": 651, \\\"upper_border\\\": 9, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"land\\\", \\\"right_border\\\": 704}\", \"{\\\"left_border\\\": 718, \\\"upper_border\\\": 8, \\\"bottom_border\\\": 31, \\\"content\\\": \\\"infertile\\\", \\\"right_border\\\": 817}\"], \"has_caption\": true}"
是因为[""]中的双引号,和\\\"应该是\"