Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JSON获取值?_Java_Android_Json - Fatal编程技术网

Java 如何使用JSON获取值?

Java 如何使用JSON获取值?,java,android,json,Java,Android,Json,如何为其他空间的存储获取“价值”?谢谢你你可以这样做: Log.d("Hot Text:", response.toString()); try { JSONArray array = response.getJSONArray("rows"); JSONObject jsonObject = array.getJSONObject(0); JSONAr

如何为其他空间的存储获取“价值”?谢谢你

你可以这样做:

            Log.d("Hot Text:", response.toString());
            try {
                JSONArray array = response.getJSONArray("rows");
                JSONObject jsonObject = array.getJSONObject(0);
                JSONArray a = jsonObject.getJSONArray("elements");
                JSONObject js = a.getJSONObject(0);
                JSONObject b = js.getJSONObject("distance");
                value = b.getString("value");
                Log.d("TAG", "title:" + value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

你能解释一下你到底想问什么吗?当我使用TRY-CATCH时,有一个值,但在结束后它是空的。请分享你的json,我会检查这个?units=imperial&origins=23.575593119.580992&destinations=23.573528119.572441&key=AIzaSyCZU4V9ST8HqM46KrQilnJv6iLrukUaIqk
String text;
int value;
try {
     JSONObject jsonObject = new JSONObject(body);
     JSONArray rowArray = jsonObject.optJSONArray("rows");
     if(rowArray.length() > 0){
       JSONObject row = rowArray.optJSONObject(0);
       JSONArray elementArray = row.optJSONArray("elements");
       if(elementArray.length() > 0){
         JSONObject element = elementArray.optJSONObject(0);
         JSONObject distance = element.optJSONObject("distance");
         text = distance.optString("text");
         value = distance.optInt("value");
       }
     }
  } catch (JSONException e) {
    e.printStackTrace();
  }
Log.e("text", text);
Log.e("value", "value : " + value);