分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

json数据解析转文本方法

发布时间:2023-09-06 02:18责任编辑:蔡小小关键词:jsjson

需要用到LitJSON.dll

JsonMapper

using LitJson;using System; public class Person{ ???// C# 3.0 auto-implemented properties ???public string ??Name ????{ get; set; } ???public int ?????Age ?????{ get; set; } ???public DateTime Birthday { get; set; }} public class JsonSample{ ???public static void Main() ???{ ???????PersonToJson(); ???????JsonToPerson(); ???} ????public static void PersonToJson() ???{ ???????Person bill = new Person(); ????????bill.Name = "William Shakespeare"; ???????bill.Age ?= 51; ???????bill.Birthday = new DateTime(1564, 4, 26); ????????string json_bill = JsonMapper.ToJson(bill); ????????Console.WriteLine(json_bill); ???} ????public static void JsonToPerson() ???{ ???????string json = @" ???????????{ ???????????????""Name"" ????: ""Thomas More"", ???????????????""Age"" ?????: 57, ???????????????""Birthday"" : ""02/07/1478 00:00:00"" ???????????}"; ????????Person thomas = JsonMapper.ToObject<Person>(json); ????????Console.WriteLine("Thomas‘ age: {0}", thomas.Age); ???}}

JsonMapper.ToObject

using LitJson;using System; public class JsonSample{ ???public static void Main() ???{ ???????string json = @" ?????????{ ???????????""album"" : { ?????????????""name"" ??: ""The Dark Side of the Moon"", ?????????????""artist"" : ""Pink Floyd"", ?????????????""year"" ??: 1973, ?????????????""tracks"" : [ ???????????????""Speak To Me"", ???????????????""Breathe"", ???????????????""On The Run"" ?????????????] ???????????} ?????????} ???????"; ????????LoadAlbumData(json); ???} ????public static void LoadAlbumData(string json_text) ???{ ???????Console.WriteLine("Reading data from the following JSON string: {0}", ?????????????????????????json_text); ????????JsonData data = JsonMapper.ToObject(json_text); ????????// Dictionaries are accessed like a hash-table ???????Console.WriteLine("Album‘s name: {0}", data["album"]["name"]); ????????// Scalar elements stored in a JsonData instance can be cast to ???????// their natural types ???????string artist = (string) data["album"]["artist"]; ???????int ???year ??= (int) data["album"]["year"]; ????????Console.WriteLine("Recorded by {0} in {1}", artist, year); ????????// Arrays are accessed like regular lists as well ???????Console.WriteLine("First track: {0}", data["album"]["tracks"][0]); ???}}

JsonReader


using LitJson;
using System;
 
public class DataReader
{
    public static void Main()
    {
        string sample = @"{
            ""name""  : ""Bill"",
            ""age""   : 32,
            ""awake"" : true,
            ""n""     : 1994.0226,
            ""note""  : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ]
          }";
 
        PrintJson(sample);
    }
 
    public static void PrintJson(string json)
    {
        JsonReader reader = new JsonReader(json);
 
        Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type");
        Console.WriteLine (new String (‘-‘, 42));
 
        // The Read() method returns false when there‘s nothing else to read
        while (reader.Read()) {
            string type = reader.Value != null ?
                reader.Value.GetType().ToString() : "";
 
            Console.WriteLine("{0,14} {1,10} {2,16}",
                              reader.Token, reader.Value, type);
        }
    }
}

json数据解析转文本方法

原文地址:https://www.cnblogs.com/VR-1024/p/9815656.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved