C # Parse JSON file Instance learning __c#

Source: Internet
Author: User
JSON syntax rule data in a key-value pair the data is saved by a comma-delimited curly brace the object brackets Save the array json is simply the objects and arrays in JavaScript, so the architecture is an object and array of two structures that can represent a variety of complex data structure 1. Object: Object in JS is represented as "{}" in the content, the data structure is {Key:value,key:value,...}
The structure of the key-value pairs, in object-oriented language, key is the object's attribute, value is the corresponding attribute value, so it is easy to understand that the value method is the object. Key (C # Object [key]) Gets the value of the property, which can be a number, string, array, and several types of objects.
2. Array: The array in JS is [] The bracketed content, the data structure is ["Java", "JavaScript",...], takes the value way and all languages, uses the index obtains, the field value type may be the number, the string, the array, the object several.

Through the object, array two kinds of data structure can be combined into complex data structure. JSON file content//////////////////////////[{"id": 2, "name": "Unparalleled", "damage": 123}, {"id": 3, "name": "No Thieves", "damage": ", {" id
": 4," name ":" Zhichitianya "," Damage ": 900}]/////////////////////////parsing code using System;
Using Litjson;
Using System.IO; Internal class Test {public static void Main () {Jsondata jdata = Jsonmapper.toobject (File.readalltext ("T
        ExtFile1.txt ")); Read file foreach (Jsondata data in jdata)//Traverse data {String id = data["id"].
            ToString (); String name = data["Name"].
            ToString (); String damage = data["damage"].
        ToString ();    Console.WriteLine (Id+name+damage);
Parsing mode two: using System by generics;
Using Litjson;

Using System.IO;
    Internal class Skill {public int id;
    public string name;

    public int damage; public override string ToString () {return string.
    Format ("ID: {0}, Name: {1}, Damage: {2}", Id, Name, Damage); } internal class Test {public static void Main () {skill[] all = Jsonmapper.toobject<skill[]> (Fi Le.
        ReadAllText ("TextFile1.txt")); When using generics, the fields in the data structure must correspond to the Object field name one by one in the JSON file, so you can find the name assignment foreach (Skill temp in all) {Console.writeli
        NE (temp); A slightly more complicated example:////////////////////{"name": "Jiajia", "Age": "Skills": [{"id": 2, "Name": "Unparalleled", "damage": 123}, {"
ID ": 3," name ":" No thieves in the world "," Damage ":"}, {"id": 4, "name": "Zhichitianya", "Damage": 900}]}///////////////////using System;
Using System.Collections.Generic;
Using Litjson;
Using System.IO;

Using System.Security.Policy;
    Internal class Player {public string Name {get; set;}
    public int Age {get; set;}

    Public list<skill> skills {get; set;} public override string ToString () {return string.
    Format ("Name:{0},age:{1}", Name, age);
    } internal class Skill {public int id;
    public string name;

    public int damage; public override string ToString () {return string.
    Format ("ID: {0}, Name: {1}, Damage: {2}", Id, Name, Damage); } internal class Test {public static void Main () {Player all = jsonmapper.toobject<player> (File .
        ReadAllText ("TextFile1.txt"));
        When using generics, the fields in the data structure must correspond to the Object field name one by one in the JSON file, so that you can find the name assignment Console.Write (all);
        foreach (var p in All.skills) {Console.WriteLine (P); }
    }
}

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.