Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:16:49+00:00 2026-05-24T09:16:49+00:00

I am passing back a JSON object (consisting of an array of strings) from

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:16:49+00:00Added an answer on May 24, 2026 at 9:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing validation server side, If fails I am passing back json data
A user enters a date in a form, but when I pass a date
How would I tell the WCF service what KnownTypes to use when passing data
I'm deploying a simple Java app to Google App Engine. I have a simple
I have recently written a socket server in PHP that will be handling communication
I have a feeling that I am going to ask a stupid question, yet
I am working on an iPad app that is fed data via web service
I'm looking to build a cross-site bookmarklet that gets a highlighted word, passes it
I currently use custom error codes when something goes wrong with my Jquery Ajax
I have to download a ton of images and I am doing it on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.