简体   繁体   中英

Cannot parse JSON to Java(android)

My PHP file:

<?php
include("ConnectDatabase.php");

$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);

$q = mysql_query("SELECT Username, Password FROM Users
                where Username = '".$Username."' and
                Password = '".$Password."'", $con);

if(mysql_num_rows($q) > 0){
    $row = mysql_fetch_assoc($q);
    print json_encode($row);


}else{
    print "0";
}


?>

I tried to parse that by the following to get a value,but it got null values both userJson and passJson:

public void parseJson(String result){

        try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                 userJson = jArray.getJSONObject(i).getString("Username").toString();
                 passJson = jArray.getJSONObject(i).getString("Password").toString();   
            }
        }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }

Anyone can see my mistake? Thank you

PS This is my older post that related to this post.

I would think they end up null cuz the first line doesn't work. It's not a JSONArray as you have generated.

   try {
      JSONObject root = new JSONObject(result);
      username = root.getString("Username");
      password = root.getString("Password");
   } catch (JSONException e) {
      Log.e("log_tag", "Error parsing data "+e.toString());
   }

something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM