-1

I am using json_encode to create JSON string in PHP. $result is my SQL result.

        $final_op="";
        if(empty($result) == false){

            $rows = array();
            while($r = mysqli_fetch_assoc($result)) {
                $rows[] = $r;
            }
            $final_op=json_encode($rows);
        }

when I get this json string at the client end I get it like

 [{"username":"vikasdevde","user_fname":"Vikas Devde","user_work":"Programmer"}]

And I am parsing it in Java as below

                 try{

                        JSONObject jObject = new JSONObject(response);
                        String aJsonUsername = jObject.getString("username");
                        String aJsonFname = jObject.getString("user_fname");
                        String aJsonUserWork = jObject.getString("user_work");

                  }catch(JSONException e){
                        e.printStackTrace();
                        System.out.println("JSON Exception");
                    }

But I'm getting a JSONException, may be it is because of the square brackets around, because after some research I tried with hard-coded string

  "{\"username\":\"vikasdevde\",\"user_fname\":\"Vikas Devde\",\"user_work\":\"Programmer\"}"

and it worked. Could you please let me know what should be the best way to handle this?

vikas devde
  • 11,691
  • 10
  • 35
  • 42
  • I don't know how to read this in java, but you are sending an array. You have to parse it as so in JAVA. Edit: in your code, you seem to be trying to read it only as a simple object, not an array os objects. – FirstOne Sep 09 '17 at 14:15
  • Is it expected that you will always get exactly one row from the result set? – Rajdeep Paul Sep 09 '17 at 14:17
  • @FirstOne yes that is what I'm asking here, it is being sent as array, and I'm looking for best way to handle this. – vikas devde Sep 09 '17 at 14:18
  • You should use a JSONArray to iterate over top-level items and fetch every array item as JSONObject. – Mateusz Sip Sep 09 '17 at 14:19
  • @RajdeepPaul For this instance I'm getting only one row in the result, but if you could also provide some info on how it is done with multiple that might help me or anyone else reading. – vikas devde Sep 09 '17 at 14:20

3 Answers3

3
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
    JSONObject jsonobject = jsonarray.getJSONObject(i);
    String username= jsonobject.getString("username");
    String user_fname= jsonobject.getString("user_fname");
    String user_work= jsonobject.getString("user_work");
}

That is the right way to go.

0

Try this -

json_encode($rows, JSON_FORCE_OBJECT);

For more reference, check this link

Nikhil G
  • 2,096
  • 14
  • 18
0

Try changing your java code like this

JSONArray jsonArray= new JSONArray(response);
JSONObject jObject=jsonArray.getJSONObject(0);
String aJsonUsername = jObject.getString("username");
String aJsonFname = jObject.getString("user_fname");
String aJsonUserWork = jObject.getString("user_work"); 

replace code in your try block with this

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44