json-json|fastjson|jsonobject
阿新 • • 發佈:2018-12-14
JSON
[
{
"cardName":"bankCard1",
"cardCode":"888888888",
"cardValue":99999999
},{
"cardName":"bankCard1",
"cardCode":"999999999",
"cardValue":222222222
}
]
阿里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);