问题描述
我的PHP成功地从我的SQL表返回JSON。但我的Android code店这一切在一个字符串。应该它确实被存储在一个字符串,然后解析出到多个对象?下面是相关的code。
My PHP successfully returns JSON from my SQL table. But my Android code stores it all in one String. Should it indeed be stored in one string and then parsed out into multiple objects? Here's the relevant code.
一,什么所存储的结果看起来像目前:
First, what the stored result looks like currently:
04-04 21:26:00.542: V/line(1230): [{"category":"elections","id":"0","title":"Who will you vote for in November's Presidential election?","published":"2012-04-02","enddate":"2012-04-30","responsetype":"0"},{"category":"elections","id":"2","title":"Question title, ladies and gents","published":"2012-04-02","enddate":"2012-04-30","responsetype":"1"}]
所以这显然都只是商店为一个大字符串,即使它的这两行。这里是code产生该行对我来说:
So that obviously all just stores as one huge string, even though it's two table rows. Here is the code that produces that line for me:
public JSONObject getQuestionJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} 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) {
Log.v("while", line);
sb.append(line + "\n");
//Log.v("err", line);
}
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;
}
很显然,我得到了各种错误,我while循环之后现在。我花了几天试图梳理出来,但只是刚刚得到的PHP返回正确的JSON(它没有previously)。我希望我需要一个JSONArray,与每个JSON结果被存储在阵列中的一个索引中的 - 所以我期望需要改变这种方法的一个JSONArray的回报,是正确的?谁能带我沿着正确的路径分析,我从我的PHP脚本收到JSON?
Obviously I get all sorts of errors after my WHILE loop right now. I've spent a couple days trying to sort it out, but only just gotten the PHP to return proper JSON (which it didn't previously). I expect I need a JSONArray, with each of the JSON results being stored inside one index of the Array - so I expect to need to change the return of this method to a JSONArray, is that correct? Can anyone lead me down the right path for parsing the JSON that I receive from my PHP script?
推荐答案
是的,这是正确的。你需要分析它的JSONArray,因为这是它是什么。例如(忽略异常等):
Yes, this is correct. You need to parse it as JSONArray, as that's what it is. For example (ignoring exceptions etc.):
JSONArray jarr = new JSONArray(jsonString);
for (int i = 0; i < jarr.length(); ++i) {
JSONObject jobj = jarr.getJSONObject(i);
// work with object
}
这篇关于JSON解析成JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!