gpt4 book ai didi

java - 解析数据时出错 org.json.JSONException : Expected ':' after n

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:37 24 4
gpt4 key购买 nike

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

解析数据 org.json.JSONException 时出错:{n“24h_avg”的字符 5 处的 n 后应为“:”:334.22,n“ask”:335.96,n“bid”:335.7,n“last” ": 335.84,n "timestamp": "2014 年 12 月 23 日星期二 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
}

最佳答案

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

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

这是因为你在这里每行的末尾附加了一个“n”:

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

换行符“\n”:

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

关于java - 解析数据时出错 org.json.JSONException : Expected ':' after n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27628943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com