Json.net的简单使用

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。

JSON 值可以是:
  • 数字(整数或浮点数)
  • 字符串(在双引号中)
  • 逻辑值(true 或 false)
  • 数组(在方括号中)
  • 对象(在花括号中)
  • null
json。net的文档!!

http://james.newtonking.com/json/help/index.html


下面是我的一些代码


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"{'NetworkAddress':'0xABCD','MyAddress':'0x1234','Voltage':[0.61,1.59,2.62],'Current':'8.31'}";

            OneCell onecell = JsonConvert.DeserializeObject<OneCell>(json);//Json字符串转换为类

            Console.WriteLine(json);

            Console.WriteLine(onecell.MyAddress + "  " + onecell.Current + "  " + onecell.NetworkAddress + "  " + onecell.Voltage[0] + "  " + onecell.Voltage[1] + "  " + onecell.Voltage[2]);

            for (int i = 4; i < 150; i++)
            {
                onecell.Voltage[0] = 0.11 + i;

                onecell.Voltage[1] = 0.21 + i;

                onecell.Voltage[2] = 0.31 + i;

                onecell.Current = i;

                onecell.MyAddress = onecell.NetworkAddress = "0xfuck";

                json = JsonConvert.SerializeObject(onecell);//类转Json字符串

                Regex reg = new Regex(@"\[.*\]");

                Match match = reg.Match(json);

                string value = match.Value.Trim('[', ']');

                Console.WriteLine(value);
            }  
        }
    }

    public class OneCell  //例子类
    {
        public string NetworkAddress { get;set;}//

        public string MyAddress { get;set;}

        public List<double> Voltage { get; set; }

        public double Current { get; set; }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值