繁体   English   中英

使用Json.Net对JSON字符串进行Json反序列化的问题

[英]Problem with Json Deserialization of html string using Json.Net

我不是json的专家,实际上我上周开始使用json,但发现自己很难将以下Json映射到一个类中。

到目前为止,我已经将来自浏览器的数据复制到了不同的Json文件中(隔离数据以查看问题),并且我意识到Config属性使反序列化过程变得混乱。 如果我(1)抓取全部数据,并且(2)将其放入文件中,并且(3)添加扩展名.json ,然后以后(4)只是擦除Config属性,则所有内容都像一个魅力。 但是我希望能够反序列化整个过程。

如我在上一篇文章中使用GetStringAsync()提到的那样,为了读取url,我使用Flurl从响应中生成字符串,并为生成类,我只是将FlurlPostman的响应粘贴到Json2Csharp转换器中 现在,对于反序列化,我已经尝试使用Json.Net进行以下操作。

//Test 1
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );

//Test 2
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");            
FlurlResponse= FlurlResponse.Replace("\\\\", "\\");
var myObj = JsonConvert.DeserializeObject<MyModel>(FlurlResponse);```

//Test 3
FlurlResponse= FlurlResponse.Replace("\\\\\\", "\\\\");            
string cleanseStr = Regex.Unescape(FlurlResponse);
var myObj = JsonConvert.DeserializeObject<MyModel>(cleanseStr );

到目前为止,我还没有运气。 我在json 开头末尾说“无法将字符串转换为[MyModel]”时收到错误 我还从Test1Test2中获取了响应值(如果没有被接受),并使用“ Json”到“ Unserialized print_r”作为我的设置将其添加到此反序列化器中,并将其与我的反序列化进行了手动比较(在使用JsonConvert.DeserializeObject之前) ),我得到了相同的结果。

目前,我一直在想自己缺少什么,以便从Config属性正确地反序列化 Json中的HTML字符串 过去曾经遇到过相同问题的人可能可以帮助我反序列化或提供任何建议。

Error: Newtonsoft.Json.JsonSerializationException: 'Error converting value blah and 4millions characters later... "value":"<span fontWeight=\" to type TestingAPI.MyModel'. Path '', line 1, position 250 (for Test2) or 1950 (for Test1 and 3, 1950 means the end of the file).

ConfigProperty.json,真正重要的是格式而不是字符串中的单词,我将所有符号保留为原始属性。

"{\"Config\":[{\"id\":1,\"description\":\"Title\",\"value\":\"blah, blah\"},{\"id\":2,\"description\":\"Dislcaimer\",\"value\":\"<span fontWeight=\\\"bold\\\"> blah- </span>\\r\\n<br/><br/>blah 101 bl.ah. <span fontWeight=\\\"bold\\\"> blah (blah) blah 101 blah\\r\\n blah blah (blah)</span> blah, blah,\\r\\n blah. blah.\\r\\n blah, blah. blah, blah\\r\\n blah.\\r\\n<br/><br/>blah, blah, blah, blah blah\\r\\n blah blah-ah blah. blah (bl-ah blah, bl-ah blah, bl-ah blah, and >blah)\\r\\n blah.\\r\\n<br/><br/>blah, blah\\r\\n blah. \\r\\n<br/><br/>blah:<a 
href=\\\"http://blah.blah.blah/blah/blah/blah.htm#blah\\\">http://blah.blah.blah/blah/blah/blah.htm#blah</a>\"}]}"

编辑

我用一种不太愉快的方式手动反序列化解决了我的问题:

string cleanseStr= Regex.Unescape(FlurlResponseString);
cleanseStr= cleanseStr.Replace("\r\n", "");
cleanseStr= cleanseStr.Replace("\\\\\\", "\\\\");
cleanseStr= cleanseStr.Replace("=\"", "=\\\"");
cleanseStr= cleanseStr.Remove(0, 1);
cleanseStr= cleanseStr.Remove(scapedJson.Length - 1, 1);
MyModel _myModel= new MyModel();
JsonConvert.PopulateObject(cleanseStr, _myModel);

@BrianRogers提出了正确的方法,谢谢@BrianRogers。

我认为这里的真正问题是原始JSON是双序列化的 也就是说,它已从对象序列化为JSON,然后再次序列化 JSON字符串,从而在字符串中产生了额外的反斜杠和引号。

因此,要反序列化,您需要执行相反的操作:将下载的双序列化JSON反序列化为普通的JSON字符串,然后将该字符串反序列化为一个对象。

我能够使用以下代码(Flurl.Http + Json.Net)成功完成此操作:

string json = await "https://gis.cdc.gov/grasp/fluView6/GetFlu6AllDataP".GetStringAsync();
json = JsonConvert.DeserializeObject<string>(json);
MyModel model = JsonConvert.DeserializeObject<MyModel>(json);

使用以下模型类:

public class MyModel
{
    public List<Group> Group { get; set; }
    public List<Season> Season { get; set; }
    public List<Age> Age { get; set; }
    public List<VirusType> VirusType { get; set; }
    public List<VirusData> VirusData { get; set; }
    public List<Config> Config { get; set; }
    public List<LastWeek> LastWeek { get; set; }
}

public class Group
{
    public string Name { get; set; }
    public int ID { get; set; }
}

public class Season
{
    public int SeasonID { get; set; }
    public string Description { get; set; }
    public int StartWeek { get; set; }
    public int EndWeek { get; set; }
    public bool Enabled { get; set; }
    public string Label { get; set; }
    public bool ShowLabType { get; set; }
}

public class Age
{
    public int AgeID { get; set; }
    public string Label { get; set; }
    public string Color_HexValue { get; set; }
    public bool Enabled { get; set; }
}

public class VirusType
{
    public int VirusID { get; set; }
    public string Description { get; set; }
    public string Label { get; set; }
    public int StartMMWRID { get; set; }
    public int EndMMWRID { get; set; }
    public int DisplayOrder { get; set; }
    public string ColorName { get; set; }
    public string Color_HexValue { get; set; }
    public int LabTypeID { get; set; }
    public int SortID { get; set; }
}

public class VirusData
{
    public int VisrusID { get; set; }
    public int AgeID { get; set; }
    public int Count { get; set; }
    public int MMWRID { get; set; }
    public int Seasonid { get; set; }
    public int PublishYearWeekID { get; set; }
    public string LoadDateTime { get; set; }
}

public class Config
{
    public int ID { get; set; }
    public string Description { get; set; }
    public string Value { get; set; }
}

public class LastWeek
{
    public int MMWRID { get; set; }
    public DateTime WeekEnd { get; set; }
    public int WeekNumber { get; set; }
    public DateTime WeekStart { get; set; }
    public int Year { get; set; }
    public string YearWeek { get; set; }
    public int SeasonID { get; set; }
}

暂无
暂无

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

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