C # Json summary,

Source: Internet
Author: User

C # Json summary,

JSON formatting will occur when writing C #. The following code preliminarily summarizes how to use it.

Newtonsoft. Json. dll must be referenced in either of the following ways:

1. Use JsonSerializer

class JosnUnit    {        public string Guid { get; set; }        public string Index { get; set; }        public string Title { get; set; }        public string subTitle { get; set; }        public string Type { get; set; }        public string IsLeaf { get; set; }        public string GroupName { get; set; }        public string[] Children { get; set; }        public string UserParam { get; set; }    }

 

Using Newtonsoft. json; using Newtonsoft. json. linq; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading. tasks; namespace json parsing {class Program {static void Main (string [] args) {string path = AppDomain. currentDomain. baseDirectory + "\ signal source .txt"; StreamReader streamReader = new StreamReader (path); // JsonReader reader = new JsonTextReader (streamReader); // while (reader. read () // {// Console. writeLine (reader. value); //} JsonSerializer serializer = new JsonSerializer (); JosnUnit [] p1 = (JosnUnit []) serializer. deserialize (new JsonTextReader (streamReader), typeof (JosnUnit []);
StreamReader. dispose (); string str = p1 [1]. guid; Console. readKey () ;}// "Guid": "40", // "Index": 0, // "Title": "2 rows of computers on the North Side 3 ", // "subTitle": "", // "Type": "32", // "IsLeaf": true, // "GroupName ":"", // "Children": [//], // "UserParam": "2" class JosnUnit {public string Guid {get; set;} public string Index {get; set;} public string Title {get; set;} public string subTitle {get; set;} public string Type {get; set;} public string IsLeaf {get; set ;} public string GroupName {get; set;} public string [] Children {get; set;} public string UserParam {get; set ;}}}

Ii. Use JsonConvert

public class SignalModel    {        public string Guid { get; set; }        public int Index { get; set; }        public string Title { get; set; }        public string subTitle { get; set; }        public string Type { get; set; }        public string GroupName { get; set; }        public string Children { get; set; }        public int UserParam { get; set; }    }

 

Static void Main (string [] args) {List <SignalModel> pResult = new List <SignalModel> (); string path = AppDomain. currentDomain. baseDirectory + "\ signal source .txt"; StreamReader streamReader = new StreamReader (path); var str = streamReader. readToEnd (); streamReader. dispose (); str = str. remove (str. length-1, 1); // Delete the ending character ']' str = str. remove (0, 1); // Delete the start character '['if (! String. IsNullOrEmpty (str) & str. Contains ("subTitle ")){
// Remove '[]' str = str from the string. replace ("[", "\" "); str = str. replace ("]", "\" "); str =" ["+ str +"] "; // Add '[]' List <SignalModel> modelList = JsonConvert at the beginning and end. deserializeObject <List <SignalModel> (str); pResult = modelList;} Console. readKey ();}

In the second method, JsonConvert is used. I found that the strings can contain '[]' At the beginning, but cannot be in the middle. Otherwise, an error is reported, but the first method is normal, later, we found that the Children field in the original string should be of the set type because it contains "[]". However, my SignalModel class only defines it as a string type. Of course, an error will be reported if the type is Json. In the first type, Children is defined as an array in the JosnUnit class, so no error is reported.

The above is a pitfall when you step on the level dishes, for your reference only.

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.