找到你要的答案

Q:How to retrieve the json array in android?

Q:如何获取Android JSON数组?

Below is my Android code which is supposed to retrieve the json array. But it shows an exception that "json array cannot be converted to json object". What is wrong with this code?

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

// testing works ...

// ... until here
for (int i = 0; i < jArray.length(); i++) {
    json = jArray.getJSONObject(i);

    String id = null, loc = null;
    id = json.getString("user_id");

    loc = json.getString("crtloc_lat");

    HashMap<String,String> persons2 = new HashMap<String,String>();
    persons2.put("uder_id",id);
    persons2.put("crtloc_lat",loc);
    personList.add(persons2);
    mylist.add(persons2);
    Toast.makeText(MapsActivity.this, "waw id is"+id, Toast.LENGTH_SHORT).show();
}

PHP file

 <?php
require "config.php";
$con = mysqli_connect(HOST,USER,PASS,DB);

$pro_id=2;
$sql="SELECT user.user_id, current_location.crtloc_lat,current_location.crtloc_lng FROM user INNER JOIN current_location 
where user.user_id=current_location.user_id AND user.pro_id='$pro_id'";



  $result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

    //create an array
    $emparray[] = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($con);
?>

and the following is my json array

[[],{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769547","crtloc_lng":"72.361504"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"},{"user_id":"86","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]

下面是我的代码应该是获取JSON数组。但它显示了一个例外,“JSON数组不能转换为JSON对象”。这个代码怎么了?

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

// testing works ...

// ... until here
for (int i = 0; i < jArray.length(); i++) {
    json = jArray.getJSONObject(i);

    String id = null, loc = null;
    id = json.getString("user_id");

    loc = json.getString("crtloc_lat");

    HashMap<String,String> persons2 = new HashMap<String,String>();
    persons2.put("uder_id",id);
    persons2.put("crtloc_lat",loc);
    personList.add(persons2);
    mylist.add(persons2);
    Toast.makeText(MapsActivity.this, "waw id is"+id, Toast.LENGTH_SHORT).show();
}

php文件

 <?php
require "config.php";
$con = mysqli_connect(HOST,USER,PASS,DB);

$pro_id=2;
$sql="SELECT user.user_id, current_location.crtloc_lat,current_location.crtloc_lng FROM user INNER JOIN current_location 
where user.user_id=current_location.user_id AND user.pro_id='$pro_id'";



  $result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

    //create an array
    $emparray[] = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($con);
?>

以下是我的JSON数组

[[],{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769547","crtloc_lng":"72.361504"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"},{"user_id":"86","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]
answer1: 回答1:

You should check your JSON as string to understand where is the problem, in any case if result is a JSON Array (like [ .... ] ) your first line is wrong

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

It should be

JSONArray jArray = json.getJSONArray(result);

But to be sure you should post your JSON string...

There are also another think that makes me a little bit confused, in JAVA a JSONObject is created around an HashMap (as you can see here) and a JSONArray is very close to an Array of HashMap...

I think that what are you tring to do is a bit useless...

你应该检查你的JSON字符串明白问题出在哪里,在任何情况下,如果结果是一个JSON数组(如[…)你的第一行是错误的

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

它应该是

JSONArray jArray = json.getJSONArray(result);

但可以肯定你应该发布你的JSON字符串…

There are also another think that makes me a little bit confused, in JAVA a JSONObject is created around an HashMap (as you can see here) and a JSONArray is very close to an Array of HashMap...

我认为你要做的是一点用处…

java  android  json