添加引用,Newtonsoft.Json.dll
using Newtonsoft.Json.Linq;

//字符串转json
public static void strJson()
{
string jsonText = "{"shenzheng":"深圳","beijing":"北京","shanghai":[{"zj1":"zj11","zj2":"zj22"},"zjs"]}";

JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);//或者JObject jo = JObject.Parse(jsonText);
string zone = jo["shenzheng"].ToString();//输出 "深圳"
string zone_en = jo["shanghai"].ToString();//输出 "[{"zj1":"zj11","zj2":"zj22"},"zjs"]"
string zj1 = jo["shanghai"][1].ToString();//输出 "zjs"
Console.WriteLine(jo);
}
//对象与数组转JSON
public static void GetJsonString()
{
//初始化对象
Obj product = new Obj() { Name = "苹果", Price = 5.5 };
//序列化
string o = new JavaScriptSerializer().Serialize(product);//值:"{\"Name\":\"苹果\",\"Price\":5.5}"

//数组转json
List<Obj> products = new List<Obj>(){
new Obj(){Name="苹果",Price=5.5},
new Obj(){Name="橘子",Price=2.5},
new Obj(){Name="干柿子",Price=16.00}
};

ProductList productlist = new ProductList();
productlist.GetProducts = products;
//序列化
string os = new JavaScriptSerializer().Serialize(productlist);
//输出 "{\"GetProducts\":[{\"Name\":\"苹果\",\"Price\":5.5},{\"Name\":\"橘子\",\"Price\":2.5},{\"Name\":\"干柿子\",\"Price\":16}]}"
}

//json转对象、数组, 反序列化
public static void JSONStringToList()
{

//json格式字符串
string JsonStr = "{Name:'苹果',Price:5.5}";
JavaScriptSerializer Serializer = new JavaScriptSerializer();

//json字符串转为对象, 反序列化
Obj obj = Serializer.Deserialize<Obj>(JsonStr);
Console.Write(obj.Name + ":" + obj.Price + "\r\n");

//json格式字符串
string JsonStrs = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";

JavaScriptSerializer Serializers = new JavaScriptSerializer();

//json字符串转为数组对象, 反序列化
List<Obj> objs = Serializers.Deserialize<List<Obj>>(JsonStrs);

foreach (var item in objs)
{
Console.Write(item.Name + ":" + item.Price + "\r\n");
}
}

Java 获取当前用户的信息 java获取当前类实例

如何获取Class类的实例对象首先我们要知道获取Class类的实例的方式一共有四种这里我们会对这四种方式进行解析注意: 我们获取到Class类的实例之后将这个实例赋给一个变量,这个变量名一定不能是class也就是变量名不能是关键字方式一: 调用运行时类的class属性eg:Class clazz1 = Person.class; //获取Person类对应的Class实例 System.out.p