Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Android 如何从json中只解析json对象_Android_Arrays_Json_Parsing - Fatal编程技术网

Android 如何从json中只解析json对象

Android 如何从json中只解析json对象,android,arrays,json,parsing,Android,Arrays,Json,Parsing,//这里我放置了json文件,它只返回数组内部元素,不返回数组名称,所以我尝试解析//它,但没有数组名称,我无法解析它 //我的json { "to": "USD", "rate": 0.98087299999999999, "from": "CAD", "v": 1.961746 } //从url获取JSON的代码 public JSONObject getJSONFromUrl(String url) { // Making HTTP request

//这里我放置了json文件,它只返回数组内部元素,不返回数组名称,所以我尝试解析//它,但没有数组名称,我无法解析它

//我的json

{
"to": "USD", 
"rate": 0.98087299999999999, 
"from": "CAD", 
"v": 1.961746
}
//从url获取JSON的代码

public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//从服务器读取数据

 try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(
                            is, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    json = sb.toString();
                } catch (Exception e) {
                    Log.e("Buffer Error", "Error converting result " + e.toString());
                }

        // try parse the string to a JSON object




  try {
                    jObj = new JSONObject(json);
                } catch (JSONException e) {
                    Log.e("JSON Parser", "Error parsing data " + e.toString());
                }

            // return JSON String


    return jObj;

        }
//解析

url = "http://rate-exchange.appspot.com/currency?from=CAD&to=INR&q=5";

                JSONParser jParser = new JSONParser();
                json = jParser.getJSONFromUrl(url);


                data_to = json.getString("to");
                data_rate = json.getDouble("rate");
                data_from = json.getString("from");
                data_value =json.getDouble("v");

问题是您的
JSON

{
   "to": "USD", 
   "rate": 0.98087299999999999, 
   "from": "CAD", 
   "v": 1.961746
}
不是
JSONArray
,而是
JSONObject
,它不包含数组,只包含键值对。因此,您只需将其指定为对象并从中获取数据:

JSONObject o = new JSONObject(sourceString);
String from = o.getString("from"); // getting value CAD with key from