我正在使用rest ful服务,它会添加额外的自动编号花括号,如下所示:
{
"budgets": [
{
"id": "1",
"balance": "0",
"addedBy": "Jhon",
"NameBD": "Camping",
"limitBD": "0",
"DateBD": "2013-11-16 04:34:03"
},
{
"id": "2",
"balance": "0",
"addedBy": "Aziz",
"NameBD": "My Home",
"limitBD": "0",
"DateBD": "2013-11-16 10:33:11"
},
{
"id": "3",
"balance": "0",
"addedBy": "Ryd",
"NameBD": "My Department",
"limitBD": "0",
"DateBD": "2013-10-24 17:40:43"
}
]
}
当我尝试解析它们时,它在“预算”中没有显示任何价值 所以我试图在JSON Viewer中查看它们:
这是我在休息控制器中的PHP函数:
public function allbudget_get()
{
$data = $this->db->get('budget');
$this->response( array('budgets'=>$data->result()) , 200);
}
有没有办法删除它们?
更新:应用代码为:
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Locate the Class
bd = new ArrayList<AllBudgets>();
// Create an array to populate the spinner
bdlist = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://localhost/SharedBudget/index.php/Budget/allbudget");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("budgets");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
AllBudgets bdget = new AllBudgets();
bdget.setId(jsonobject.optString("id"));
bdget.setAddedBy(jsonobject.optString("addedBy"));
bdget.setNameBD(jsonobject.optString("NameBD"));
bdget.setDateBD(jsonobject.optString("DateBD"));
bdget.add(bdget);
// Populate spinner with budget names
bdlist.add(jsonobject.optString("NameBD"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
以下是用于解析的JSON代码
公共类JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
更新LOGCAT 我也有安装人员和吸气但我知道它们不是logcat显示的问题:
11-18 21:32:39.680: D/InputTransport(27646): Input channel constructed: name='ClientState{43f51ee0 uid 10152 pid 27646} (server)', fd=59
11-18 21:32:40.760: E/Error(27646): No value for budgets
11-18 21:32:40.760: W/System.err(27646): org.json.JSONException: No value for budgets
11-18 21:32:40.765: W/System.err(27646): at org.json.JSONObject.get(JSONObject.java:354)
11-18 21:32:40.765: W/System.err(27646): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
11-18 21:32:40.765: W/System.err(27646): at com.androidbegin.jsonspinnertutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:49)
11-18 21:32:40.765: W/System.err(27646): at com.androidbegin.jsonspinnertutorial.MainActivity$DownloadJSON.doInBackground(MainActivity.java:1)
11-18 21:32:40.765: W/System.err(27646): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-18 21:32:40.765: W/System.err(27646): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-18 21:32:40.765: W/System.err(27646): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-18 21:32:40.765: W/System.err(27646): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-18 21:32:40.765: W/System.err(27646): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-18 21:32:40.765: W/System.err(27646): at java.lang.Thread.run(Thread.java:841)
11-18 21:32:41.230: D/dalvikvm(27646): GC_FOR_ALLOC freed 214K, 16% free 10193K/12064K, paused 20ms, total 21ms
11-18 21:32:41.230: I/dalvikvm-heap(27646): Grow heap (frag case) to 12.641MB for 1127536-byte allocation
11-18 21:32:41.255: D/dalvikvm(27646): GC_FOR_ALLOC freed <1K, 15% free 11294K/13168K, paused 22ms, total 22ms
11-18 21:32:41.265: D/AbsListView(27646): Get MotionRecognitionManager
11-18 21:32:41.275: D/InputTransport(27646): Input channel constructed: name='44913558 PopupWindow:42738418 (client)', fd=56
11-18 21:32:41.300: D/AbsListView(27646): unregisterIRListener() is called
11-18 21:32:41.305: D/AbsListView(27646): unregisterIRListener() is called
11-18 21:32:41.310: D/AbsListView(27646): unregisterIRListener() is called
11-18 21:33:18.115: D/AbsListView(27646): unregisterIRListener() is called
答案 0 :(得分:0)
我找到了答案,我使用了httpPost,而函数使用了GET,所以我只是替换了它:
HttpGet httpget = new HttpGet(url);
感谢所有试图提供帮助的人!