user836200
user836200

Reputation: 655

Problems creating JSON arrays from php JSON object

I am passing back a JSON object (consisting of an array of strings) from php. I am trying to convert the object into a Java jArray but the string I get back from my php file isn't formed correctly:

String from php (I have 2 entries in my db tips table)

[{"0":"2","id":"2","1":"2","household_id":"2","2":"3","stepgreen_id":"3","3":"tip 1","tip":"tip 1","4":"2011-08-05","dateOfTip":"2011-08-05","5":"3","likes":"3"}]

[{"0":"2","id":"2","1":"2","household_id":"2","2":"3","stepgreen_id":"3","3":"tip 1","tip":"tip 1","4":"2011-08-05","dateOfTip":"2011-08-05","5":"3","likes":"3"},{"0":"91","id":"91","1":"1","household_id":"1","2":"1","stepgreen_id":"1","3":"tip 2","tip":"tip 2","4":"2011-08-04","dateOfTip":"2011-08-04","5":"1","likes":"1"}]

Here is my php code

    <?php
    // mysql connection, etc....
    $query  = "SELECT * FROM Tips";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
    $output[]=$row;
    print(json_encode($output));
    } 
    mysql_close($con);
    ?>

In Java, I do the following. What happens is that I get back a jArray with only one entry. I expect 2. I'm not sure why the php json object is returning an array of 1 string and a second array of 2 strings. I only expect to receive an array of 2 strings.

     try {
      InputStream responseData;
      responseData = httpEntity.getContent();
      js = convertStreamToString(responseData);
      Log.v(LOG_TAG, js);
      jArray = new JSONArray(js);

      JSONObject json_data = null;

      Log.v(LOG_TAG, "Arraysize: " + jArray.length());
         for (int i = 0; i < jArray.length(); i++) {
        Log.v(LOG_TAG, "entering loop");
        json_data = jArray.getJSONObject(i);
        tip = json_data.getString("tip");
        stepgreenId = json_data.getString("stepgreen_id");
      dateOfTip = json_data.getString("dateOfTip");
      householdId = json_data.getString("household_id");
      likes = json_data.getString("likes");
      Log.v(LOG_TAG, "Json tip= " + tip + " stepgreen id: " + stepgreenId +  " household_id: " + householdId + " likes: " + likes);

    }
} catch (IllegalStateException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Upvotes: 0

Views: 690

Answers (4)

Marc B
Marc B

Reputation: 360632

You've got your print_r() inside your while() loop, so you're dumping out a json string for every row in the query result set, and this string keeps growing as you add more records. This means if you get 5 rows, you'll have 5 json strings dumped out.

Most likely Java is taking only the FIRST of those json strings (the first/single record one) and dumping the rest on the floor.

Change your code to this and things should start working:

while($row = mysql_fetch_array($result)) {
    $output[] = $row;
}
print(json_encode($output));

Upvotes: 0

Dutchie432
Dutchie432

Reputation: 29160

the proper way to do this is to return a json string with both (or more) objects in it.

$first_array = array(...);
$second_array = array(...);

die(json_encode(array(
    $first_array,
    $second_array
)));

I would also suggest using jQuery's getJSON() function. it simply returns a JS object you can easily read/manipulate. api.jquery.com/jQuery.getJSON

$.getJSON('yourFeedPage.php', 'id=xyz123', function(obj) {
    for (int i = 0; i < obj.length(); i++) {
        var item = obj[i];

        tip = item.tip;
        stepgreenId = item.stepgreen_id;
        dateOfTip = item.dateOfTip;
        householdId = item.household_id;
        likes = item.likes;
    }
);

Upvotes: 0

Chris Carson
Chris Carson

Reputation: 1845

Don't know much java, but on the PHP side you should try using mysql_fetch_assoc or mysql_fetch_object instead. mysql_fetch_array fetches numeric and string keys which are duplicate, and the numeric ones may be causing the problem in java.

Upvotes: 0

Nemanja Miljković
Nemanja Miljković

Reputation: 997

If you do the following:

$first_array = array('first array');
$second_array = array('second array');

echo json_encode($first_array);
echo json_encode($second_array);

The JSON that you will get is invalid if you want to deserialize it. You have to create a common array for them and then print it out. In your case:

<?php
// mysql connection, etc....
$query  = "SELECT * FROM Tips";
$result = mysql_query($query);
$output = array();
while($row = mysql_fetch_array($result))
    $output[]=$row;

print(json_encode($output));
mysql_close($con);

This should work.

Upvotes: 1

Related Questions