JSONException:无法使用JSON Array的帮助转换为JSONObject
$ Error :: JSONException:在JSON数组的帮助下,json字符串无法转换为JSONObject
这是导入库
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.os.AsyncTask; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast;
activity_main.xml文件
互联网许可就在这里
error
请给我答案如何使用此代码获取多个值并显示到我的ListView中
这是我的MainActivity.java类文件。
public class MainActivity extends Activity { private String jsonResult; private String url = "YOUR_PHP_FILE_URL"; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); accessWebService(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } // Async Task to access the web @SuppressLint("NewApi") private class JsonReadTask extends AsyncTask { @Override protected String doInBackground(String... params) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(params[0]); try { HttpResponse response = httpclient.execute(httppost); jsonResult = inputStreamToString(response.getEntity().getContent()).toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } private StringBuilder inputStreamToString(InputStream is) { String rLine = ""; StringBuilder answer = new StringBuilder(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); try { while ((rLine = rd.readLine()) != null) { answer.append(rLine); Log.d(answer.toString(), "String Builder Class in IF Loop"); } } catch (IOException e) { // e.printStackTrace(); Toast.makeText(getApplicationContext(),"Error..." + e.toString(), Toast.LENGTH_LONG).show(); Log.d(answer.toString(), "String Builder Class in Else Part"); } return answer; } @Override protected void onPostExecute(String result) { ListDrwaer(); } }// end async task public void accessWebService() { JsonReadTask task = new JsonReadTask(); // passes values for the urls string array task.execute(new String[] { url }); } // build hash set for list view public void ListDrwaer() { ArrayList<Map> userList = new ArrayList<Map>(); Log.d(userList.toString(), "Starting JSONObject"); try { Log.d("Starting Try Block", "Before JSONObject"); JSONObject jsonResponse = new JSONObject(jsonResult); JSONArray jsonMainNode = jsonResponse.getJSONArray("user_info"); Log.d(jsonMainNode.toString(), "Starting JSONObject"); for (int i = 0; i < jsonMainNode.length(); i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); String id = jsonChildNode.optString("Id:"); String name = jsonChildNode.optString("Name:"); String email = jsonChildNode.optString("Email:"); String phone = jsonChildNode.optString("Phone:"); String password = jsonChildNode.optString("Password"); String outPut = id + "-" + name+ "-" + email+ "-" + phone+ "-" + password; userList.add(createEmployee("user_info", outPut)); Log.d(jsonChildNode.toString(), "Starting JSONObject inside For Loop"); } } catch (JSONException e) { Toast.makeText(getApplicationContext(), "Error" + e.toString(),Toast.LENGTH_SHORT).show(); Log.e("log_tag", "Failed data was:\n" + jsonResult); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, userList,android.R.layout.simple_list_item_1,new String[] { "user_info" }, new int[] { android.R.id.text1 }); listView.setAdapter(simpleAdapter); } private HashMap createEmployee(String name, String number) { HashMap employeeNameNo = new HashMap(); employeeNameNo.put(name, number); return employeeNameNo; } }
好的,你得到的错误是非常通用的,错误本身告诉你做错了什么
$ Error :: JSONException:在JSON数组的帮助下,json字符串无法转换为JSONObject,
我相信你在JSON数据中有字符串, "dummy data"
因为它是双引号,你试图读作JSONObject
。 它不是JSONObject
而是String
基本类型,
在解析JSON
您需要注意什么是Object
, Array
和primitive-type
JSONObject
将始终包含在{ }
因此这表示括号内的数据是JSONObject,例如===>
JSONObject json = jsonArray.getJSONObject(i)
要么
JSONObject json = new JSONObject("JSON DATA"); JSONArray
将始终包含在[ ]
,这表示方括号内的数据是JSONArray,例如==>`json.getJSONArray(“jsonArray的名称”);
Primitive-type
Boolean
会喜欢这个
"isSelected":true or "isSelected":false
Integer
是这样的
"someInt":12
String
会这样
"someString":"String value"
正如您可以看到问题所在,您需要区分JSON
String和JSON Object
删除此行是因为您将Json String转换为Json Object
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);