Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/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?_Android_Json - Fatal编程技术网

如何在android中解析简单的JSON?

如何在android中解析简单的JSON?,android,json,Android,Json,我想在android java中使用这段代码。但是json解析器不接受此数据作为json。这不是json吗。?但是服务器api正在发送这些数据,我不明白这是什么以及如何处理 { "result": "success", "income_today": "$0.00 USD", "income_thismonth": "$0.00 USD", "income_thisyear": "$0.00 USD", "orde

我想在android java中使用这段代码。但是json解析器不接受此数据作为json。这不是json吗。?但是服务器api正在发送这些数据,我不明白这是什么以及如何处理

 {
        "result": "success",
        "income_today": "$0.00 USD",
        "income_thismonth": "$0.00 USD",
        "income_thisyear": "$0.00 USD",
        "orders_pending": "0",
        "orders_today_cancelled": 0,
        "orders_today_pending": 0,
        "orders_today_fraud": 0,
        "orders_today_active": 0,
        "orders_today_total": 0,
        "orders_yesterday_cancelled": 0,
        "orders_yesterday_pending": 0,
        "orders_yesterday_fraud": 0,
        "orders_yesterday_active": 0,
        "orders_yesterday_total": 0,
        "orders_thismonth_total": "0",
        "orders_thisyear_total": "0",
        "tickets_open": "0",
        "tickets_answered": "4",
        "tickets_customerreply": "0",
        "tickets_onhold": "0",
        "tickets_inprogress": "0",
        "tickets_closed": "1",
        "tickets_allactive": 4,
        "tickets_awaitingreply": 0,
        "tickets_flaggedtickets": "0",
        "cancellations_pending": "0",
        "todoitems_due": "114",
        "networkissues_open": "0",
        "billableitems_uninvoiced": "0",
        "quotes_valid": "0",
        "staff_online": "1"
    }
我的解析器是

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                Log.d("url", url);
                Log.d("params", params.toString());
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                Log.d("param", paramString);
                if (!paramString.isEmpty()) {
                    url += "?" + paramString;
                }
                Log.d("url", url);
                HttpGet httpGet = new HttpGet(url);
                Log.d("httpGet", httpGet.toString());
                HttpResponse httpResponse = httpClient.execute(httpGet);
                Log.d("httpResponse", httpResponse.toString());
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                Log.d("is", is.toString());
            }

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

    }
}

这确实是有效的json数据。 您应该尝试以下方法:


这是一个JSON,你能告诉我你是如何使用解析器来解析它的吗?这确实是有效的JSON数据。你能提供你正在使用的代码来解析它吗?这是有效的JSON,问题在于你的code@Duggu然后它是一个整数,并且在中是有效值json@Duggu0是一个完全有效的字符串,除非OP尝试将其解析为int,否则一切都应该正常。