json-json|fastjson|jsonobject

版权声明:fromZjy QQ1045152332 https://blog.csdn.net/qq_36762677/article/details/82969011

一、JSON

JavaScript 对象表示法(JavaScript Object Notation)

1.JSON 对象

{ 
	"name":"菜鸟" ,
	 "age":18
 }
 
{ "flag":true }

{ "runoob":null }

1.JSON 数组

{
	"sites": [
	{ "name":"xiaoming" , "age":15}, 
	{ "name":"xiaohong" , "age":14}
	]
}

二、阿里fastjson

 	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.15</version>
	</dependency>
import com.alibaba.fastjson.JSON; 
import com.alibaba.fastjson.JSONObject;

JSON对象生成字符串

    JSONObject jObject=new JSONObject();
    jObject.put("username", "xiejava");
    jObject.put("sex", "man");
    jObject.put("age", 38); 
   	jObject.put("email", "[email protected]");
   	String json2=jObject.toJSONString();

通过JSONArray包装对象数组

List<Card> cards=new ArrayList<Card>();
cards.add(card1);
cards.add(card2);
JSONArray jArray=new JSONArray();
jArray.addAll(cards);

json字符串转java对象(OrderInfo(orderId、orderDate))

OrderInfo orderInfo = JSON.parseObject(jsonStr, OrderInfo.class);

json字符串转json对象

String jsonStr ={\”orderId\” : \”1111111\”;\”orderDate\” : \”2016-11-22 11:03:00\”}; 
JSONObject jsonObject = JSON.parseObject(jsonStr); 
String orderId = jsonObject.getString(“orderId”); 
String orderDate =jsonObject.getString(“orderDate”);

java对象转json字符串

//java对象转json字符串
String jsonStr = JSON.toJSONString(orderInfo)
//java对象转json对象
JSON json = (JSON) JSON.toJSON(orderInfo);

猜你喜欢

转载自blog.csdn.net/qq_36762677/article/details/82969011