【问题标题】:of type org.json.JSONArray cannot be converted to JSONObjectorg.json.JSONArray 类型的无法转换为 JSONObject
【发布时间】:2023-03-08 00:03:01
【问题描述】:

我在 php 中将数据库中的姓名、电子邮件 ID、ph 号显示到列表视图中。

enter code here

     public class MainActivity extends ListActivity
     {
// url to make request
private static String url = "http://10.0.2.2/programs/get_data.php";
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_phno = "phno";
    JSONArray contacts;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

   // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = ArrayList<HashMap<String,string();       
    // Creating JSON Parser instance

    JSONParser jParser = new JSONParser();
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);  
    try {
    // Getting Array of Contacts
    contacts = json.getJSONArray(TAG_CONTACTS);
    // looping through All Contacts
    for(int i = 0; i < contacts.length(); i++)
    {
    JSONObject c = contacts.getJSONObject(i);
    String name = c.getString("name");
    String phno = c.getString("phno");
    String email = c.getString("id");
    System.out.println("hi");
    // creating new HashMap
    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TAG_NAME, name);
    map.put(TAG_ID,email);
    map.put(TAG_phno, phno);
    // adding HashList to ArrayList
    contactList.add(map);
    }
   }catch (JSONException e) {
   e.printStackTrace();
    }
    ListAdapter adapter = new SimpleAdapter(this, contactList,
    R.layout.list_item,
    new String[] { TAG_NAME, TAG_ID, TAG_phno }, new int[] {
    R.id.name, R.id.email, R.id.phno });
    setListAdapter(adapter);

    }

    }   






public class JSONParser  extends Activity
    {

static InputStream is;
static JSONObject jObj;
static String json = "";
   // constructor
public JSONParser() 
{

}

public JSONObject getJSONFromUrl(String url)
{


// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
    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) {
sb.append(line + "\n");
}
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;

  }
  }

我收到错误

11-21 22:44:00.306: E/JSON Parser(17671): 解析数据时出错 org.json.JSONException: Value [{"id":"c.com","phno":"12", "name":"Chaitra D"},{"id":"s.com","phno":"1254","name":"Shrivatsa K"},{"id":"m.com", "phno":"1456","name":"Mahalaxmi B"},{"id":"Sh.com","phno":"1234","name":"Sharadha"},{"id" :"shh.com","phno":"1564","name":"shrilatha"},{"id":"n.com","phno":"97","name":"namratha h "},{"id":"shri.com","phno":"167","name":"Shrinivas D"},{"id":"p.com","phno":"987" org.json.JSONArray 类型的 "name":"PG sunita"}] 无法转换为 JSONObject

请帮帮我,谢谢

【问题讨论】:

    标签: php android json


    【解决方案1】:

    你得到这个异常是因为你没有一个 json 对象,你有一个 json 对象的 json 数组。首先,将你的字符串解析为一个 json 数组,然后遍历数组以获取每个对象。

    【讨论】:

      【解决方案2】:

      我猜来自服务器的 json 是一个数组,所以你应该从那个数组中获取第一个 JSONObject。 就像:

      public JSONArray getJSONFromUrl(String url) { 
       // get JSONArray... }
      
      jsonArray = getJSONFromUrl(Url); JSONOBject jsonObject = jsonArray.getJSONObject(0);
      

      或者更简单的方法是使用此库从服务器获取 JSON:http://loopj.com/android-async-http/

      AsyncHttpClient client = new AsyncHttpClient();
      client.get(stringURL, paramsOrNull, new JsonHttpResponseHandler() {
      @Override
                  public void onStart() {
                          // do somethings when start send request to server
                  }
      
                  @Override
                  public void onFailure(Throwable arg0) {
                          // do somethings when fail to request or receive respone from server
                  }
      
                  @Override
                  public void onSuccess(JSONObject rawJson) {
                          // do somethings when sucessful...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-13
        • 2018-02-04
        • 2019-12-29
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 2018-05-12
        相关资源
        最近更新 更多