【问题标题】:Error parsing data org.json.JSONException: Expected ':' after n解析数据 org.json.JSONException 时出错:在 n 之后应为 ':'
【发布时间】:2014-12-23 22:27:04
【问题描述】:

所以我试图解析一个 JSONObject 但我不断收到错误:

Error parsing data org.json.JSONException: Expected ':' after n at character 5 of {n "24h_avg": 334.22,n "ask": 335.96,n "bid": 335.7,n "last": 335.84,n "timestamp": "Tue, 23 Dec 2014 22:13:55 -0000",n "volume_btc": 30328.82,n "volume_percent": 82.62n}n

我能够从服务器获取我需要的 json,但由于某些奇怪的原因它没有被正确解析。

这是我的 MainActivity.java

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONObject;

public class MainActivity extends ActionBarActivity {

public static final String URL = "https://api.bitcoinaverage.com/ticker/global/USD/";
TextView mPriceText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(isNetworkAvailable()) {
        JSONParse parse = new JSONParse();
        parse.execute();
    }
}

private boolean isNetworkAvailable() {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();

    boolean isAvailable = false;
    if(networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
    }

    return isAvailable;
}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting price ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJsonFromUrl(URL);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // jokes = json.getJSONArray(TAG_JOKE);
            // getting json from url
            //JSONObject c = json.getJ();
            // store json item
            String price = (String) json.get("24h_avg");
            // set json data in textview
            mPriceText = (TextView) findViewById(R.id.priceTextView);
            mPriceText.setText(price);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的 JSON 解析器类

public class JSONParser {

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

public JSONObject getJsonFromUrl(String url) {

    // Make request
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch(IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line;
        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 {
        // try parsing the string to a JSON object
        jObj = new JSONObject(json);
    } catch(Exception e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return json string
    return jObj;
}

最后,这是我要解析的 json。

{
  "24h_avg": 333.8,
  "ask": 337.79,
  "bid": 337.31,
  "last": 337.77,
  "timestamp": "Tue, 23 Dec 2014 20:06:17 -0000",
  "volume_btc": 29261.88,
  "volume_percent": 81.98
}

【问题讨论】:

    标签: java android json parsing


    【解决方案1】:

    在错误信息中注意奇怪的“n”个字符:

    解析数据 org.json.JSONException 时出错:在 {n "24h_avg": 334.22,n "ask": 335.96,n "bid": 335.7,n "last": 335.84 ,n "timestamp": "Tue, 23 Dec 2014 22:13:55 -0000",n "volume_btc": 30328.82,n "volume_percent": 82.62n}n

    这是因为您在此处的每一行末尾附加了一个“n”:

        while((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }
    

    换行符“\n”:

        while((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    

    【讨论】:

      【解决方案2】:

      您在行阅读循环中有错字。将sb.append(line + "n"); 替换为sb.append(line + "\n");

      如果您想避免在 line + "\n" 中发生额外的不可见 StringBuilder 分配,您应该这样做 sb.append(line).append("\n");

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-31
        相关资源
        最近更新 更多