简体   繁体   English

尝试使用 JSON.NET 转换特定的 Json 会给我错误转换值

[英]Trying to convert specific Json with JSON.NET gives me Error Converting Value

["

I am actually implementing some new functionality for my company's app, the previous dev already did some similar deserialization with JSON.NET successfully and I was trying to make it work with my new functionality.<\/i>我实际上正在为我公司的应用程序实现一些新功能,以前的开发人员已经成功地使用 JSON.NET 进行了一些类似的反序列化,我正试图让它与我的新功能一起使用。<\/b><\/p>

Here is the object I am trying to deserialize:<\/i>这是我要反序列化的对象:<\/b><\/p>

"{
 \"@type\":[\"IN.history.1\"],
 \"@self\":\"/history\",
 \"history\":[
  \"195166208;
  2021-09-06T11:48:26.164;
  User Modified;
  TA_NONE;
  1.1.System.4.1;
  \\u0003D\\u0001 = \\u0001\\n\\u0006 = Standard\\nIsMandatory = No\\nModifiedUser_ID = 3\\nModifiedUser_Name = BIS Benutzer\\nModifierUser_ID = 3\\nModifierUser_Name = BIS Benutzer\\nSource_Name = RPS\\nSource_ReportingNumber = 0\\n\"
 ]
}"

You cannot deserialize HistoryList like that.你不能像那样反序列HistoryList It's just an array containing a single string, there is no inherent meaning in them being particular properties.它只是一个包含单个字符串的数组,它们作为特定属性没有内在含义。 Instead you will have to parse it out manually:相反,您将不得不手动解析它:

public class GetHistory
{
    public List<string> type { get; set; }
    public string self { get; set; }
    public List<string> history { get; set; }
}
var history = JsonConvert.DeserializeObject<MAP5000Classes.GetHistory>(response);
var strings = history.history[0].Split(new[]{';', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
return new HistoryList {
    eventID = strings[0],
    timestamp = strings[1],
    eventName = strings[2],
    ta = strings[3],
    siid = strings[4],
    parameters = strings[5],
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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