简体   繁体   English

如何仅解析json中的json对象

[英]how to parse only json object from the json

//here i put my json file and it return only array inner elements not array name so i tried to parse //it but without array name i couldn't do it //这里我放了json文件,它只返回数组内部元素,而不返回数组名称,因此我尝试解析//但没有数组名称,我做不到

//my json //我的json

{
"to": "USD", 
"rate": 0.98087299999999999, 
"from": "CAD", 
"v": 1.961746
}

//code to getJson from url //从网址获取getJson的代码

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();
        }

// read data from server //从服务器读取数据

 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;

        }

// parse //解析

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");

Problem is that your JSON : 问题是您的JSON

{
   "to": "USD", 
   "rate": 0.98087299999999999, 
   "from": "CAD", 
   "v": 1.961746
}

Is not JSONArray but JSONObject that doesn't contain Array but only key-value pairs. 不是JSONArray而是不包含Array而是仅包含键值对的JSONObject So you need just assign it as Object and get data from it: 因此,您只需要将其分配为Object并从中获取数据即可:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM