所以我试图解析一个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 Parser类
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
}
答案 0 :(得分:3)
在错误消息中注意奇怪的“n”字符:
解析数据时出错org.json.JSONException:在{n“24h_avg”的字符5处的n之后预期':':334.22,n“ask”:335.96,n“bid”:335.7,n“last”:335.84 ,n“timestamp”:“Tue,2014年12月23日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");
}
答案 1 :(得分:0)
你在阅读循环中输了一个错字。将sb.append(line + "n");
替换为sb.append(line + "\n");
如果你想避免在line + "\n"
中发生额外的隐形StringBuilder分配,你应该做sb.append(line).append("\n");