我正在尝试解析JSON并且它显示了error.I使用另一个JSON url测试了我的代码并且它工作正常。我的代码详细信息,
URL - JSON URL
public class Home extends Activity {
// url to make request
private static String url = "http://ads.mocean.mobi/ad?site=38206&zone=156431&type=2&size_x=57&size_y=57&count=5&ua=Mozilla/5.0%20(Windows%20NT%206.2;%20WOW64)%20AppleWebKit/537.22%20(KHTML,%20like%20Gecko)%20Chrome/25.0.1364.152%20Safari/537.22%20117.223.16.66&jsvar=test&type=2&key=6";
// JSON Node names
private static final String APP_ARRAYNAME = "var test ";
private static final String APP_URL = "url";
private static final String APP_NAME = "text";
private static final String APP_IMAGE = "img";
private static final String IMAGE_TYPE = "type";
private static final String APP_TRACK = "track";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home);
if (Utils.isNetworkAvailable(Home.this)) {
new MyTask().execute(url);
} else {
showToast("No Network Connection!!!");
}
}
class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Home.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (null == result || result.length() == 0) {
showToast("No data found from web!!!");
//Home.this.finish();
} else {
try {
JSONObject mainJson = new JSONObject(result);
//JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
// System.out.println(c.getInt(ID));
System.out.println(c.getString(APP_URL));
System.out.println(c.getString(APP_NAME));
System.out.println(c.getString(APP_IMAGE));
// System.out.println(c.getInt(AGE));
System.out.println(c.getString(IMAGE_TYPE));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public void showToast(String msg) {
Toast.makeText(Home.this, msg, Toast.LENGTH_LONG).show();
}
}
public class Utils {
public static String getJSONString(String url) {
String jsonString = null;
HttpURLConnection linkConnection = null;
try {
URL linkurl = new URL(url);
linkConnection = (HttpURLConnection) linkurl.openConnection();
int responseCode = linkConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream linkinStream = linkConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while ((j = linkinStream.read()) != -1) {
baos.write(j);
}
byte[] data = baos.toByteArray();
jsonString = new String(data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (linkConnection != null) {
linkConnection.disconnect();
}
}
Log.i("JSON Response----", ""+jsonString);
return jsonString;
}
public static boolean isNetworkAvailable(Activity activity) {
ConnectivityManager connectivity = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
请建议我解析这种JSON的解决方案。
注意 - :这款Json在iPhone上经过了充分测试,运行良好。
04-24 12:09:55.270: I/JSON Response----(2385): var test = [{"url" : "http://ads.mocean.mobi/2/redir/c5c61601-aca9-11e2-8400-a0369f167751/0/306865","text" : "Instagram","img" : "http://img.ads.mocean.mobi/img/50e/cbf/01b1baf/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61601-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61602-aca9-11e2-8400-a0369f167751/0/306834","text" : "Drawsomething","img" : "http://img.ads.mocean.mobi/img/50e/cbe/2f70a73/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61602-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61604-aca9-11e2-8400-a0369f167751/0/306518","text" : "Google Maps","img" : "http://img.ads.mocean.mobi/img/50e/c99/b95edc9/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61604-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c61605-aca9-11e2-8400-a0369f167751/0/306765","text" : "Find Friends","img" : "http://img.ads.mocean.mobi/img/50e/cb5/ce11bc6/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c61605-aca9-11e2-8400-a0369f167751"},{"url" : "http://ads.mocean.mobi/2/redir/c5c63d10-aca9-11e2-8400-a0369f167751/0/306733","text" : "Flashlight","img" : "http://img.ads.mocean.mobi/img/50e/cb3/c34c71a/image_57x57.png","type": "image/png","track" : "http://ads.mocean.mobi/2/img/c5c63d10-aca9-11e2-8400-a0369f167751"}];
04-24 12:09:55.290: W/System.err(2385): org.json.JSONException: Value var of type java.lang.String cannot be converted to JSONObject
04-24 12:09:55.290: W/System.err(2385): at org.json.JSON.typeMismatch(JSON.java:111)
04-24 12:09:55.290: W/System.err(2385): at org.json.JSONObject.<init>(JSONObject.java:158)
04-24 12:09:55.301: W/System.err(2385): at org.json.JSONObject.<init>(JSONObject.java:171)
04-24 12:09:55.301: W/System.err(2385): at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:153)
04-24 12:09:55.301: W/System.err(2385): at com.impressol.appjiva.Home$MyTask.onPostExecute(Home.java:1)
04-24 12:09:55.310: W/System.err(2385): at android.os.AsyncTask.finish(AsyncTask.java:602)
04-24 12:09:55.310: W/System.err(2385): at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-24 12:09:55.320: W/System.err(2385): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-24 12:09:55.330: W/System.err(2385): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 12:09:55.330: W/System.err(2385): at android.os.Looper.loop(Looper.java:137)
04-24 12:09:55.330: W/System.err(2385): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-24 12:09:55.330: W/System.err(2385): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 12:09:55.341: W/System.err(2385): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 12:09:55.341: W/System.err(2385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 12:09:55.341: W/System.err(2385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 12:09:55.350: W/System.err(2385): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
从头开始删除“var test”和“;”从结果字符串的结尾。所以JSON看起来像是......
[
{
"url": "http://ads.mocean.mobi/4/redir/a01a00e0-acab-11e2-afa2-a0369f06db33/0/306833",
"text": "Amazon",
"img": "http://img.ads.mocean.mobi/img/50e/cbb/ed021d2/image_57x57.png",
"type": "image/png",
"track": "http://ads.mocean.mobi/4/img/a01a00e0-acab-11e2-afa2-a0369f06db33"
},
{
"url": "http://ads.mocean.mobi/4/redir/a01a00e2-acab-11e2-afa2-a0369f06db33/0/306864",
"text": "Call of Duty",
"img": "http://img.ads.mocean.mobi/img/50e/cbe/c0c7c88/image_57x57.png",
"type": "image/png",
"track": "http://ads.mocean.mobi/4/img/a01a00e2-acab-11e2-afa2-a0369f06db33"
},
{
"url": "http://ads.mocean.mobi/4/redir/a01a27f0-acab-11e2-afa2-a0369f06db33/0/306868",
"text": "GPS Drive",
"img": "http://img.ads.mocean.mobi/img/50e/cc0/42a71e8/image_57x57.png",
"type": "image/png",
"track": "http://ads.mocean.mobi/4/img/a01a27f0-acab-11e2-afa2-a0369f06db33"
},
{
"url": "http://ads.mocean.mobi/4/redir/a01a27f3-acab-11e2-afa2-a0369f06db33/0/306764",
"text": "Twitter",
"img": "http://img.ads.mocean.mobi/img/50e/cb5/942aaab/image_57x57.png",
"type": "image/png",
"track": "http://ads.mocean.mobi/4/img/a01a27f3-acab-11e2-afa2-a0369f06db33"
},
{
"url": "http://ads.mocean.mobi/4/redir/a01a4f00-acab-11e2-afa2-a0369f06db33/0/306829",
"text": "Temple run Brave",
"img": "http://img.ads.mocean.mobi/img/50e/cba/c30b077/image_57x57.png",
"type": "image/png",
"track": "http://ads.mocean.mobi/4/img/a01a4f00-acab-11e2-afa2-a0369f06db33"
}
]
然后尝试更改以下行:
JSONObject mainJson = new JSONObject(result);
//JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
JSONArray jsonArray = mainJson.getJSONArray(APP_ARRAYNAME);
使用
JSONArray jsonArray = new JSONArray(result);
您始终可以使用http://jsonlint.org/来检查JSON是否有效......
答案 1 :(得分:2)
在开始时移除var test =
,在JSON响应中移除;
。
您的JSON解析Android代码应该如下
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
System.out.println(c.getString(APP_URL));
System.out.println(c.getString(APP_NAME));
System.out.println(c.getString(APP_IMAGE));
System.out.println(c.getString(IMAGE_TYPE));
System.out.println(c.getString(APP_TRACK));
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 2 :(得分:1)
我认为您需要从字符串解析中删除“var test =”...