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

时间:2015-01-16 09:56:01

标签: 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",
        "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;

    }
}

1 个答案:

答案 0 :(得分:1)

这确实是有效的json数据。 你应该尝试这样的事情:

http://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html