java获取json子字段值_java解析Json中获取Array字段值及嵌套Json对象

1.   Json含有数组时,先把String类型的json串转换的Json对象,通过getJSONArray(key)方法获取其Array部分,然后通过下标和key获取相应子数组里具体的字段值:

String test_arrary = "{\"address\": [{\"addressLine1\": \"Noida\",\"addressLine2\": \"UP\"}],\"firstName\": \"Achyut\",\"lastName\": \"khanna\"}";

public static String getJsonArray(String reqJson) {

JSONObject json = null;

String address2 = null;

try {

json = new JSONObject(reqJson);

JSONArray jsonarr= json.getJSONArray("address");

String address1 = jsonarr.getJSONObject(0).getString("addressLine1");

address2 = jsonarr.getJSONObject(0).getString("addressLine2");

System.out.println("address1==" + address1);

System.out.println("address2==" + addre

你可能感兴趣的:(java获取json子字段值)