建议去官网学习:http://www.json.org/json-zh.html
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
JSON建构于两种结构:
- “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
- 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。
JSON具有以下这些形式:
对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。
Example:
首先在项目文档中创建一个Json文档的Txt。
[
{"id":2,"name":"tianxiawushuang","damage":10},
{"id":3,"name":"tianxiawudi","damage":101},
{"id":4,"name":"chichitianya","damage":110}
]
然后在实际项目中引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;
using System.Text;
using System.IO;
namespace _20171016
{
class Program
{
struct Skill
{
int Id;
int Damage;
string Name;
};
static void Main(string[] args)
//使用LitJson引用方法1:在官网上下载LitJson的dll,在项目引用中引入
//在项目引用中的NUGet管理中搜索LitJson,安装下载引用
{
//使用JsonMapper解释json文本
//jsondata代表一个数组或者对象,在这里代表一个数组
//using LitJson using System.IO
List<Skill> skilllist = new List<Skill>;
JsonData jsondata = JsonMapper.ToObject(File.ReadAllText("Json.txt"));
foreach(JsonData temp in jsondata)//在这里temp代表一个对象
{
Skill skill = new Skill();
JsonData idValue = temp["id"];//通过字符串索引器可以取得键值对的值
JsonData nameValue = temp["name"];
JsonData damageValue = temp["damage"];
int id = Int32.Parse(idValue.ToString());
int damage = Int32.Parse(damageValue.ToString());
// Console.WriteLine(id + ":" + nameValue.ToString() + ":" + damage);
skill.Id = id;
skill.Damge = damage;
skill.Name = nameValue.ToString();
skilllist.Add(skill);
}
Console.ReadKey();
}
}
当然也可以使用泛型JsonMapper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;
using System.Text;
using System.IO;
namespace _20171016
{
class Program
{
struct Skill
{
int Id;
int Damage;
string Name;
};
static void Main(string[] args)
//使用LitJson引用方法1:在官网上下载LitJson的dll,在项目引用中引入
//在项目引用中的NUGet管理中搜索LitJson,安装下载引用
{
//使用JsonMapper解释json文本
//jsondata代表一个数组或者对象,在这里代表一个数组
//using LitJson using System.IO
//List<Skill> skilllist = new List<Skill>;
//JsonData jsondata = JsonMapper.ToObject(File.ReadAllText("Json.txt"));
//foreach(JsonData temp in jsondata)//在这里temp代表一个对象
//{
// Skill skill = new Skill();
// JsonData idValue = temp["id"];//通过字符串索引器可以取得键值对的值
// JsonData nameValue = temp["name"];
// JsonData damageValue = temp["damage"];
// int id = Int32.Parse(idValue.ToString());
// int damage = Int32.Parse(damageValue.ToString());
// // Console.WriteLine(id + ":" + nameValue.ToString() + ":" + damage);
// skill.Id = id;
// skill.Damge = damage;
// skill.Name = nameValue.ToString();
// skilllist.Add(skill);
//}
//Console.ReadKey();
//}
//键必须与定义的类保持一致
Skill[] skillArray = JsonMapper.ToObject<Skill[]>(File.ReadAllText("Json.tex"));
foreach (var temp in skillArray)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
}