When you plan to fill out an Excel table, the conventions are filled in JSON format for complex types of data. For example, the content of column D is JSON data.
The JSON in the cell is as follows.
{ "name":"BeJson2", "URL":"http://www.bejson.com", "page": the, "Isnonprofit":true, "Address": { "Street":"Science Park Road.", " City":"Suzhou, Jiangsu", "Country":"China" }, "links": [{ "name":"Google", "URL":"http://www.google.com" }, { "name":"Baidu", "URL":"http://www.baidu.com" }, { "name":"SoSo", "URL":"http://www.SoSo.com" }]}
Choose to use Exceldatareader the library reads Excel, and for a cell is a JSON string, the read JSON turns the nested JSON into a string with a slash (escape character), similar to the case.
Here's the problem: the string is no longer in JSON format and cannot be deserialized correctly with the Newtonsoft.json library.
Workaround: One solution is to use a C # class as a reference when serializing JSON. This JSON cell corresponds to a C # class (not a string string) by reflecting the type of the corresponding property in the C # class.
For the JSON nested above, you can use the C # class to describe the following.
Public classuser{ Public stringID {Get;Set; } Public stringName {Get;Set; } Public stringattribute {Get;Set; } Public stringTTR {Get;Set; } PublicUserconfig User_config {Get;Set; }} Public classuserconfig{ Public stringName {Get;Set; } Public stringURL {Get;Set; } Public intPage {Get;Set; } Public BOOLIsnonprofit {Get;Set; } PublicAddress Address {Get;Set; } PublicLinks[] Links {Get;Set; }} Public classaddress{ Public stringStreet {Get;Set; } Public stringCity {Get;Set; } Public stringCountry {Get;Set; }} Public classlinks{ Public stringName {Get;Set; } Public stringURL {Get;Set; }}
C # code, when serializing JSON, determines the type of content in each cell, and the nested JSON is strongly converted to the class type of C #, and cannot be left to be a string output.
The key code that reflects the C # class and makes judgments is similar to the following:
//stores a row of row data. Key is the header attribute field and value is the cell content. dictionary<string,Object> row =Newdictionary<string,Object>(); Type FieldType=Propertyinfo.propertytype;if(Fieldtype.isprimitive &&Fieldtype.isvaluetype) { intValue =int. Parse (cell. ToString ());//value types, such as int values (conventions do not enumerate)Row[field] =value;}Else if(FieldType = =typeof(string) ) {Row[field]= Cell. ToString ();//string}Else if(fieldtype.isclass) {Objectobj = Jsonconvert.deserializeobject (cell. ToString ());//ObjectRow[field] =obj;}
This way, the exported nested JSON does not have a slash. Because it is not a string type at output, it is an object.
"C #" reads JSON objects nested in Excel, JSON with slashes (one)