zoukankan      html  css  js  c++  java
  • json串的使用

    一:在C#中使用json字符串

    从这里下载:http://www.newtonsoft.com/products/json/

    安装:

    1.解压下载文件,得到Newtonsoft.Json.dll
    2.在项目中添加引用..
    序列化和反序列在.net项目中:

    Product product = new Product();
     
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
     
    string output = JavaScriptConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "Expiry": new Date(1230422400000),
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}
     
    Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));

    读取JSON

    string jsonText = "['JSON!',1,true,{property:'value'}]";
     
    JsonReader reader = new JsonReader(new StringReader(jsonText));
     
    Console.WriteLine("TokenType		ValueType		Value");
     
    while (reader.Read())
    {
        Console.WriteLine(reader.TokenType + "		" + WriteValue(reader.ValueType) + "		" + WriteValue(reader.Value))
    }

    结果显示:

    TokenTypeValueTypeValue
    StartArray null null
    String System.String JSON!
    Integer System.Int32 1
    Boolean System.Boolean True
    StartObject null null
    PropertyName System.String property
    String System.String value
    EndObject null null
    EndArray null null

    JSON写入

    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(sw);
     
    writer.WriteStartArray();
    writer.WriteValue("JSON!");
    writer.WriteValue(1);
    writer.WriteValue(true);
    writer.WriteStartObject();
    writer.WritePropertyName("property");
    writer.WriteValue("value");
    writer.WriteEndObject();
    writer.WriteEndArray();
     
    writer.Flush();
     
    string jsonText = sw.GetStringBuilder().ToString();
     
    Console.WriteLine(jsonText);
    // ['JSON!',1,true,{property:'value'}]

    这里会打印出: ['JSON!',1,true,{property:'value'}].

    完整例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    //需要引用 Newtonsoft.Json.dll
    using Newtonsoft.Json;
     
    namespace JsonTest
    {
        class Program
        {
            /// <summary>
            /// 人员类
            /// </summary>
            public class Person
            {
                public string name; //姓名
                public int age; //年龄
                public bool sex_is_male; //性别
     
                public struct Partner //伙伴
                {
                    public string partner_name; //伙伴姓名
                    public int partner_age; //伙伴年龄
                    public bool partner_sex_is_male; //伙伴性别
                }
                public Partner partner;
     
                public string[] achievement; //成就
            }
     
            static void Main(string[] args)
            {
                //设置一个Person类
                Person p = new Person();
                p.name = "Tsybius";
                p.age = 23;
                p.sex_is_male = true;
                p.partner.partner_name = "Galatea";
                p.partner.partner_age = 21;
                p.partner.partner_sex_is_male = false;
                p.achievement = new string[] { "ach1", "ach2", "ach3" };
     
                //直接输出
                Console.WriteLine("Formatting.None:");
                string json1 = JsonConvert.SerializeObject(p);
                Console.WriteLine(json1 + "
    ");
     
                //缩进输出
                Console.WriteLine("Formatting.Indented:");
                string json2 = JsonConvert.SerializeObject(p, Formatting.Indented);
                Console.WriteLine(json2 + "
    ");
     
                Console.ReadLine();
            }
        }
    }

    二:格式

    格式:

    {"tools": [
        { "name":"css format" , "site":"http://www.cnblogs.com/hongmaju/" },
        { "name":"json format" , "site":"http://www.cnblogs.com/hongmaju/" },
        { "name":"hash MD5" , "site":"http://www.cnblogs.com/hongmaju/" }
        ]
    }

    有层次的格式:

    {
        "0": [
            {
                "Txt": "['收银员']",
                "Names": "['bi']",
                "data": [
                    {
                        "ID": "1",
                        "Name": "收银员"
                    },
                    {
                        "ID": "1",
                        "Name": "张三"
                    }
                ]
            }
        ],
        "1": [
            {
                "Txt": "['餐台类型']",
                "Names": "['ro']",
                "data": [
                    {
                        "ID": "16",
                        "Name": "A1"
                    },
                    {
                        "ID": "17",
                        "Name": "包桌"
                    }
                ]
            }
        ],
        "2": [
            {
                "Txt": "['账单类型']",
                "Names": "['bi']",
                "data": [
                    {
                        "ID": "G",
                        "Name": "挂账账单"
                    },
                    {
                        "ID": "gzhk",
                        "Name": "挂账回款"
                    },
                    {
                        "ID": "H",
                        "Name": "会员充值"
                    },
                    {
                        "ID": "l",
                        "Name": "连锁账单"
                    },
                    {
                        "ID": "M",
                        "Name": "招待账单"
                    },
                    {
                        "ID": "R",
                        "Name": "红冲账单"
                    },
                    {
                        "ID": "Z",
                        "Name": "异地卡充值"
                    },
                    {
                        "ID": "J",
                        "Name": "预订押金"
                    },
                    {
                        "ID": "Z",
                        "Name": "已结账单"
                    }
                ]
            }
        ],
        "3": [
            {
                "Txt": "['营业站点']",
                "Names": "['se']",
                "data": [
                    {
                        "ID": "10",
                        "Name": "重庆万州面馆"
                    }
                ]
            }
        ]
    }

    在线验证json格式的地址:http://www.atool.org/jsonformat.php

    三:常用的使用方法

    对上面的json的处理:

    success: function (result) {
      for (var key in result) {
        
    alert(key);//弹出0,1,2,3这几个数字
      alert(result[key]);//弹出0对应的json串
       alert(getValue(result[key][0].Txt));//得到0对应的json串中的Txt的值     for (var da in result[key]) {     alert(result[key][da].data);//得到0对应的json串中的data的json串     }   } }
  • 相关阅读:
    五,系统操作命令说明
    三,linux系统的由来
    二,服务器磁盘阵列(raid)
    一服务器硬件介绍
    四,元组类型
    集合类型
    字典类型
    三,列表类型
    二,字符串类型
    一,数字类型
  • 原文地址:https://www.cnblogs.com/hongmaju/p/5116488.html
Copyright © 2011-2022 走看看