对象转化为json格式字符串,json格式字符串转化为JSONObject对象,取出JSONObject对象里的key值

json格式字符串jsonString

{
    "code":"S0A00000",
    "data":{
        "userBaseInfo":{
            "group":{
                "companyId":"10000",
                "eshopCode":"11",
                "eshopTmp":"eshopTmp",
                "officeId":"10000",
                "userId":101
            }
,
            "role":{
                "roleCode":"crt_shzx_admin,crt_dszx_admin,crt_cxzx_admin",
                "userId":101
            }
,
            "user":{
                "firstName":"uat-admin1",
                "lastName":"uat-admin1",
                "optCounter":0,
                "userEml":"[email protected]",
                "userId":101,
                "userMbl":"18012345678",
                "userName":"uat-admin1",
                "userType":"1,2,3"
            }

        }

    }
,
    "msg":"成功",
    "transactionUuid":"b4d9a1220ddc4a8fba1bf921d218517b"
}


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

// json格式字符串jsonString转化为JSONObject对象

JSONObject jsonObj = JSON.parseObject(jsonString);

//从jsonObj对象里取出key是data的对象
JSONObject data = jsonObj.getJSONObject("data");
if(data != null){

//从data对象里取出userBaseInfo对象
JSONObject userBaseInfo = data.getJSONObject("userBaseInfo");

//从userBaseInfo对象里取出group对象
JSONObject group = userBaseInfo.getJSONObject("group");

//从group 对象里取出key值为companyId的值
Long merchantId = Long.valueOf(group.getString("companyId"));
}

你可能感兴趣的:(java)