"C #" reads JSON objects nested in Excel, JSON with slashes (one)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.