Simple use of JSON parsing data

Source: Internet
Author: User

Simply remember that JSON parsing is simple and practical:

Usage Scenario: JSON data that is uploaded to the client in the background, similar to:

string jsonobject="{' Name ': ' Jack ', ' age ': +}"; string " [{' Name ': ' Jhon ', ' age ': ' 23},{' name ': ' Jack ', ' age ':] " ;      

A client can define a class in which the properties in the class correspond to the corresponding fields in the data

class student{    publicstringgetset;}      Public int Get Set ; }}

Add Newtonsoft.json references through NuGet packages and add using Newtonsoft.Json.Linq; namespaces

Create the Jtoken object and parse the above string:

Jtoken Jtoken = jtoken.parse (jsonobject); Jtoken jtoken2=jtoken.parse (Jsonarray);

The data can then be converted to the appropriate object:

if(Jtoken1 isjobject) {Student s=jtoken. Toobject<student>();       Console.WriteLine (S.name); Console.WriteLine (S.age); }if(Jtoken2 isJarray) {List<Student> students = Jtoken2. Toobject<list<student>>(); foreach(varStudentinchstudents) {Console.WriteLine (string. Format ("Name:{0}\tage:{1}", student. Name, student.    Age)); }}

Simply introduce a way to parse the data:)

Spents: 10min

Simple use of JSON parsing data

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.