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
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; }
}
}