0

My php:

 <?php

 $con=mysqli_connect("localhost","xxx","xxx","xxx");

 $result = mysqli_query($con, "SELECT username, x,y FROM user");

 $jsonData = array();
while($array = mysqli_fetch_assoc($result)){
$jsonData[] = $array;
}

echo json_encode($jsonData);


mysqli_close($con);
?>

My Fetched data to JSONArray: [{"username":"one","x":"22.","y":"55"},{"username":"two","x":2,"y":5}]

Android code:

        //Connect to mysql.
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http:example.com/fetchCoordinates.php");

        //Getting response
        HttpResponse response = httpClient.execute(httpPost);
        String responseBody = EntityUtils.toString(response.getEntity());

        //Trying to get fetch from array.
        JSONArray array = new JSONArray(responseBody);
        JSONObject jsonObject = array.getJSONObject(0);
        double x = jsonObject.getDouble("x");

Eror:

org.json.JSONArray cannot be converted to JSONObject

It doesn't work. I'm stuck with it.

Benas.M
  • 340
  • 5
  • 14
  • What error are you getting? And also isnt JSON supposed to have { } instead of [ ] ? – Vucko May 06 '16 at 20:52
  • There's no JSON object in your JSON. – Sotirios Delimanolis May 06 '16 at 20:53
  • Vucko. I updated question. Sotirios, you mean in java or php ? – Benas.M May 06 '16 at 20:56
  • I mean your actual JSON only contains an array of arrays of strings. There's no object in there. – Sotirios Delimanolis May 06 '16 at 20:58
  • Yes, you got a point. Maybe you can suggest me sollution or give links that would help ? – Benas.M May 06 '16 at 21:06
  • Solution for what? You decided your model to not contains any JSON objects but then you're trying to retrieve JSON objects...Decide what data you want, then use it. – Sotirios Delimanolis May 06 '16 at 21:16
  • Sorry if I'm not straight. What I want to do, is to get JSONObjects into array with my php and then to get from that array objects in my java code. I thought that I already getting JSONObjects in array. Used this link to help me: http://www.kodingmadesimple.com/2015/01/convert-mysql-to-json-using-php.html – Benas.M May 06 '16 at 21:19

3 Answers3

0

Your JSON is valid, but your issue is that it does not contain a JSONObject. It's just an array of arrays of strings. To actually get JSONObjects out of it, it needs to be an array of actual JSONObjects. Try formatting your data something like this:

[{
    "name": "one",
    "num1": "22",
    "num2": "55"
}, 
{
    "name": "two",
    "num1": "2",
    "num2": "5"
}]

Notice that the array, which is contained within brackets [] contains comma separated JSONObjects, which are key-value pairs contained within curly braces {}.

You should read up on how to properly format JSON. Here is a good resource: http://www.w3schools.com/json/default.asp

Also see this answer for some helpful info: How do you represent a JSON array of strings?

Community
  • 1
  • 1
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • 1
    Thanks, good answer :) That really gave me better understanding, and most importantly it works now ;) – Benas.M May 06 '16 at 21:32
0

To create a json object from an array, the array has to be associative so you can change your code to this to archieve that.

  while($array = mysqli_fetch_assoc($result)){
       $jsonData[] = $array;
  }
Peter Chaula
  • 3,456
  • 2
  • 28
  • 32
0

The problem was in my PHP code.

What was wrong:

while($array = mysqli_fetch_rows($result)){
   $jsonData[] = $array;}

What is correct:

while($array = mysqli_fetch_assoc($result)){
   $jsonData[] = $array;}

Why?

First method (fetch_rows) gave me not JSONArray with JSONObjects inside, but instead I got JSONArray with JSONStrings inside.

In java code I tried to receive JSONObject from JSONArray, but I was getting error, because there wasn't any objects(there were strings).

Benas.M
  • 340
  • 5
  • 14