package com.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class TestJson {
public static void main(String[] args) throws InterruptedException {
getJsonKey();
Thread.currentThread().sleep(200);
getJsonArrayKey();
}
/**
* @功能描述:普通JSON
*/
public static void getJsonKey(){
String createData="{\"mydatas\":{\"price\":10,\"pname\":\"f1\",\"weight\":20,66:88}}";
System.err.println("getJsonKey:"+createData);
// 1.将JSON转成JSONObject对象
JSONObject createJsonObject = JSON.parseObject(createData.toString());
// 2.获取mydatas内容
String mydatas = createJsonObject.getString("mydatas");
System.out.println("JSON:"+mydatas);
// 3.将子集也转成JSONObject对象
JSONObject product = JSON.parseObject(mydatas);
// 4.获取子集内容
String price = product.getString("price");
String pname = product.getString("pname");
String weight = product.getString("weight");
String m66 = product.getString("66");
System.out.print("price:"+price);
System.out.print(" pname:"+pname);
System.out.print(" weight:"+weight);
System.out.println(" 66:"+m66);
}
/**
* @功能描述:JSON数组
*/
public static void getJsonArrayKey(){
// 测试JSON
String createData="{\"MEMBER_INFO\": [{\"CARD_NAME\":
\"普通客户\",\"MEM_PROD_LIST\": {},\"BRAND_NAME\": \"动感地带\",\"MEMBER_ID\":
10057268560,\"CUST_NAME\": \"**静\"}, {\"CARD_NAME\":
\"普通客户\",\"MEM_PROD_LIST\": {},\"BRAND_NAME\": \"神州行\",\"MEMBER_ID\":
60053724236,\"CUST_NAME\": \"**娟\"}],\"GROUP_TYPE\":
\"SNTY\",\"LOCK_FLAG\": \"0\",\"GRPID_NO\":
11310020302802,\"GROUP_CODE\": \"11310020302802\"}";
System.err.println("getJsonArryKey:"+createData);
// 1.将JSON转成JSONObject对象
JSONObject createJsonObject = JSON.parseObject(createData.toString());
// 2.获取MEMBER_INFO内容
JSONArray ja = createJsonObject.getJSONArray("MEMBER_INFO");
JSONObject jaObject =(JSONObject) ja.get(1);
System.out.println("ADDPROD_NAME:"+jaObject.getString("ADDPROD_NAME"));
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。