使用Json.Net对JSON字符串进行Json反序列化的问题 [英] Problem with Json Deserialization of html string using Json.Net

查看:91
本文介绍了使用Json.Net对JSON字符串进行Json反序列化的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是json专家,实际上我上周开始使用json,但发现自己很难在以下

I'm not an expert with json, actually I started last week working with it and I found myself having trouble trying to get map the following Json into a class.

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

S far I have copy the data from the browser into different Json files (isolate data to see the problem) and I realize that the property Config mess the deserialization process. If I (1) grab the entire data and (2) put it into a file and (3) add the extension .json and then later (4) just erase the Config property, everything works like a charm. But I want to be able to deserialize the whole thing.

如我在上一个 Json2Csharp转换器.现在,对于反序列化,我已经尝试使用Json.Net进行以下操作.

For reading the url I'm using Flurl to generate a string from the response as I mentioned in a previous post using the GetStringAsync() and for generating the Classes I just paste the response from Flurl or Postman into a Json2Csharp converter. Now for Deserialization I have try the following using 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]".我还从 Test1 Test2 中获取了响应值(如果没有被接受),并将其添加到作为我的设置,并将它们与我的反序列化进行手工比较(在使用JsonConvert.DeserializeObject之前),我得到了相同的结果.

So far, I had no luck. I get errors at the beginning of the json or at the end saying that "cannot convert string into [MyModel]". I also took the response value from Test1 and Test2 (if I'm not istaken) and add it to this unserializer using "Json" to "Unserialized print_r" as my settings and compare them to my deserialization by hand (before using JsonConvert.DeserializeObject) and I got the same result.

此刻,我一直在想 要在Config属性中的Json中正确反序列化HTML字符串 缺少的内容.过去曾经遇到过相同问题的人可能可以帮助我反序列化此问题或提供任何建议.

At the moment I'm stuck thinking what I'm missing in order to correctly deserialize the HTML string within the Json from the Config property. Probably someone that had face the same problem in the past can help me deserialize this or give any advice.

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>\"}]}"

编辑

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

EDIT

I solved my problem in a not very pleasant way, manual deserialization:

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.

The correct way is proposed by @BrianRogers, thank you @BrianRogers.

推荐答案

我认为真正的问题是原始JSON是 double-serialized .也就是说,将其从对象序列化为JSON,然后将该JSON字符串再次序列化 ,从而在字符串中产生额外的反斜杠和引号.

I think the real problem here is that the original JSON is double-serialized. That is, it was serialized from an object to JSON and then that JSON string was serialized again, resulting in extra backslashes and quotes in the string.

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

So, to deserialize it, you need to do the reverse: deserialize the downloaded double-serialized JSON to a normal JSON string, then deserialize that string to an object.

我能够使用以下代码(Flurl.Http + Json.Net)成功做到这一点:

I was able to do this successfully using the following code (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; }
}

这篇关于使用Json.Net对JSON字符串进行Json反序列化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆