前言
最近初接觸
C#
語言捶牢,發(fā)現(xiàn)JSON
解析這塊和JAVA
差異過大,所以針對簡單的JSON
操作巍耗,如String
秋麸、int
、Array
常用類型進(jìn)行演示炬太、留存灸蟆,便于以后的使用。
使用的庫
本文使用到了
Newtonsoft.Json
庫亲族,使用時可直接在NuGet程序包
中進(jìn)行安裝炒考。
- 使用到的命名空間如下:
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
生成JSON
手動生成
public static String generateJsonStr()
{
//生成JSON
JObject rootJ = new JObject();
rootJ["className"] = "六年級一班";
rootJ["classTeacher"] = "張欣悅";
JArray datas = new JArray();
//生成第1條數(shù)據(jù)
JObject itemJ1 = new JObject();
itemJ1["name"] = "張?zhí)烀?;
itemJ1["sex"] = "男";
itemJ1["age"] = 14;
JArray majorCouse1 = new JArray();
majorCouse1.Add("語文");
majorCouse1.Add("數(shù)學(xué)");
majorCouse1.Add("音樂");
itemJ1["majorCourse"] = majorCouse1;
datas.Add(itemJ1);
//生成第2條數(shù)據(jù)
JObject itemJ2 = new JObject();
itemJ2["name"] = "劉一一";
itemJ2["sex"] = "女";
itemJ2["age"] = 15;
JArray majorCouse2 = new JArray();
majorCouse2.Add("語文");
majorCouse2.Add("數(shù)學(xué)");
majorCouse2.Add("舞蹈");
itemJ2["majorCourse"] = majorCouse2;
datas.Add(itemJ2);
rootJ["datas"] = datas;
return rootJ.ToString();
}
序列化對象
using Newtonsoft.Json;
//將對象序列化為JSON數(shù)據(jù)
String JsonStr = JsonConvert.SerializeObject(studentBean);
解析JSON
手動解析
注意:此處轉(zhuǎn)換為
List
集合時,不可將包含多種類型的數(shù)組轉(zhuǎn)換為List
集合霎迫,會失斦唷!目前使用的數(shù)據(jù)格式如:"majorCourse": ["語文","數(shù)學(xué)","音樂"]
JObject rootJ = JObject.Parse(JsonStr);
String className = rootJ.Value<String>("className");
String classTeacher = rootJ.Value<String>("classTeacher");
JArray rootJA = rootJ.Value<JArray>("datas");
foreach (JObject itemJ in rootJA)
{
String name = itemJ.Value<String>("name");
String sex = itemJ.Value<String>("sex");
String age = itemJ.Value<String>("age");
//直接將Strijng類型的Json數(shù)組轉(zhuǎn)換為List集合
List<String> majorCourse = itemJ.Value<JArray>("majorCourse").ToObject<List<String>>();
printLog("name="+ name + ",sex="+sex + ",age=" + age + majorCourse[0]);
foreach(String item in majorCourse)
{
printLog("課程:" + item);
}
}
項目中使用的數(shù)據(jù)
生成的JSON數(shù)據(jù)
{
"className": "六年級一班",
"classTeacher": "張欣悅",
"datas": [
{
"name": "張?zhí)烀?,
"sex":"男",
"age":14,
"majorCourse": [
"語文",
"數(shù)學(xué)",
"音樂"
]
},
{
"name": "劉一一",
"sex": "女",
"age": 15,
"majorCourse": [
"語文",
"數(shù)學(xué)",
"舞蹈"
]
}
]
}
源碼
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsFormsApp1.json.bean;
namespace WindowsFormsApp1.json
{
class JsonDemo
{
public static void JsonDemoStart()
{
String jsonStr = generateJsonStr();
printLog("生成的JSON數(shù)據(jù)為:" + jsonStr.ToString());
//反序列化為對象Bean
StudentBean studentBean = JsonConvert.DeserializeObject<StudentBean>(jsonStr);
//獲取數(shù)據(jù)
printLog("班主任姓名為:" + studentBean.classTeacher);
printLog("學(xué)生信息為:" + studentBean.datas[0]);
//將對象序列化為JSON數(shù)據(jù)
String JsonStr = JsonConvert.SerializeObject(studentBean);
printLog("序列化Json數(shù)據(jù):" + JsonStr);
//手動JSON解析
JObject rootJ = JObject.Parse(JsonStr);
String className = rootJ.Value<String>("className");
String classTeacher = rootJ.Value<String>("classTeacher");
JArray rootJA = rootJ.Value<JArray>("datas");
foreach (JObject itemJ in rootJA)
{
String name = itemJ.Value<String>("name");
String sex = itemJ.Value<String>("sex");
String age = itemJ.Value<String>("age");
//直接將Strijng類型的Json數(shù)組轉(zhuǎn)換為List集合
List<String> majorCourse = itemJ.Value<JArray>("majorCourse").ToObject<List<String>>();
printLog("name="+ name + ",sex="+sex + ",age=" + age + majorCourse[0]);
foreach(String item in majorCourse)
{
printLog("課程:" + item);
}
}
}
public static String generateJsonStr()
{
//生成JSON
JObject rootJ = new JObject();
rootJ["className"] = "六年級一班";
rootJ["classTeacher"] = "張欣悅";
JArray datas = new JArray();
//生成第1條數(shù)據(jù)
JObject itemJ1 = new JObject();
itemJ1["name"] = "張?zhí)烀?;
itemJ1["sex"] = "男";
itemJ1["age"] = 14;
JArray majorCouse1 = new JArray();
majorCouse1.Add("語文");
majorCouse1.Add("數(shù)學(xué)");
majorCouse1.Add("音樂");
itemJ1["majorCourse"] = majorCouse1;
datas.Add(itemJ1);
//生成第2條數(shù)據(jù)
JObject itemJ2 = new JObject();
itemJ2["name"] = "劉一一";
itemJ2["sex"] = "女";
itemJ2["age"] = 15;
JArray majorCouse2 = new JArray();
majorCouse2.Add("語文");
majorCouse2.Add("數(shù)學(xué)");
majorCouse2.Add("舞蹈");
itemJ2["majorCourse"] = majorCouse2;
datas.Add(itemJ2);
rootJ["datas"] = datas;
return rootJ.ToString();
}
public static void printLog(String log)
{
Console.WriteLine(log);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp1.json.bean
{
class StudentBean
{
public String className
{
get;
set;
}
public String classTeacher
{
get;
set;
}
public List<StudentItemBean> datas
{
get;
set;
}
}
class StudentItemBean
{
public String name
{
get;
set;
}
public String sex
{
get;
set;
}
public int age
{
get;
set;
}
public List<String> majorCourse
{
get;
set;
}
public override String ToString()
{
return String.Format("[name={0},sex={1},age={2},majorCourse={3}]", name, sex, age, majorCourse);
}
}
}