JSON语法错误的php功能?

时间:2011-12-14 09:50:38

标签: java php android json

我在Android应用程序中解析我的JSon Data时遇到问题。我使用PHP脚本从mysql获取数据并将其放入JSON中。问题是我可以打印结果,我看到我桌子上的所有项目的人。

JSON data from database

但是当数据传递给stringbuilder时一切都会出错。解析JSON的数组是否说我的数组中只有一个对象? 我希望你能帮助我或给我一些建议。

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.1.10/getAllPeople.php");
    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    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);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");
       String line="0";
       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());
        }
//paring data
int id;
double Lat;
double Long;
String hulp = "";

v3 = (TextView) findViewById(R.id.tv3);
//v3.setText("Resultaat: " + result);

try{
      jArray = new JSONArray(result);
      for(int i=0;i<jArray.length();i++){                 
             JSONObject json_data = jArray.getJSONObject(i);
             id = json_data.getInt("id");
             Long = json_data.getDouble("long");
             Lat = json_data.getDouble("lat");
             hulp = hulp + "\n" +  "Long: " + Long + " Lat: " + Lat;
             mob.add(new MobieleFlitsers(id,Long,Lat));
         }

      for(int i=0;i<mob.size();i++){

          hulp = " " + mob.size();
        }
        v3.setText(hulp);
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "Geen locatie's gevonden" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }

我添加了PHP代码,因为它存储了错误的JSON格式。

        $q=mysql_query("SELECT * FROM people");

    //while($e=mysql_fetch_assoc($q))
    //$output[]=$e;
    //print(json_encode($output));
    //mysql_close();

    if (!$q) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
    }

    if (mysql_num_rows($q) == 0) {
        echo "No rows found, nothing to print so am exiting";
        exit;
    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    while ($row = mysql_fetch_row($q)) {
        $output[]=$row;
        print(json_encode($output));


    }
    mysql_close();
}

}

&GT;

3 个答案:

答案 0 :(得分:2)

看了你的图片后,看来你的JSON无效:

[{"id": 1, "long": 5000, "long": 5000}][{"id": 1, "long": 5000, "long": 5001}]

这应该是:

[{"id": 1, "long": 5000, "long": 5000}, {"id": 1, "long": 5000, "long": 5001}]

请注意,在您当前的代码中,每个 Object都包含在方括号中,而应该只有一个Array包含所有Object s以逗号分隔。

尝试将您的JSON输出放入JSONLint以仔细检查其有效性。

答案 1 :(得分:1)

在你的PHP中完成jakeclarkson的回答

while ($row = mysql_fetch_row($q)) {
    $output[]=$row;
    print(json_encode($output));
}

打印到目前为止每行的整个json ,所以基本上,它打印[1] [1,2] [1,2,3] ...而不是只打印最后一行。您只需在$ output完成后提取您的打印:

while ($row = mysql_fetch_row($q)) {
    $output[]=$row;
}
print(json_encode($output));

答案 2 :(得分:0)

数组中只有一个对象。如果仔细观察JSON,你会发现它包含连续的数组,每个数组都包含单个对象。语法错了。

基本上你在第一个对象之后终止了数组。