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 7601199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:06:40+00:00 2026-05-30T23:06:40+00:00

I am trying to fetch data from database using php and send it to

  • 0

I am trying to fetch data from database using php and send it to java using json. Using java, I wanted to retrieve json values from an arraylist. That arrayList will contain Fruit Object. But in my code, I get values inside arraylist and that arraylist contains LinkedHashMap and not the fruit object. How do I change the php code so that in java side I could get values like ArrayList fruitList = new ArrayList();

<?php
    require("phpfunc.fns");
    include('Fruit.php');
    sqlconnect();

    $data = array();
    $fruit = new Fruit();
    $sql="select * from fruit";         
    $result=mysql_query( $sql);
    $count = 0;

    while( $r=mysql_fetch_array($result) ){
        //echo $r['lati'].",". $r['longi'] ;
        $fruit->setFruitId($r['fruitId']);
        $fruit->setAvailable($r['available']);

        $data[$count] = $fruit;
        $count++;
    }
    echo json_encode($data);

?>

  //java code
public void decodeUsingGSON(String json){
        Gson gson = new Gson();
        ArrayList fruitList = gson.fromJson(json, ArrayList.class);

        for(int i=0; i<fruitList.size(); i++){
            Fruit fruit = (Fruit) fruitList.get(i);
            System.out.println(fruit.getAvailable());
        }
    }
  • 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-30T23:06:41+00:00Added an answer on May 30, 2026 at 11:06 pm

    You should read up on the Gson documentation. It describes how to handle generic data types and collections. The following should work for you, assuming properly formed JSON:

    final Type collectionType = new TypeToken<Collection<Fruit>>(){}.getType();
    final Collection<Fruit> fruits = gson.fromJson(json, collectionType);
    
    for(final Fruit fruit; fruits) {
        System.out.println(fruit.getAvailable());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to fetch data from a div (based on his id), using PHP's
I am trying to export data from database to an excel sheet, using php
I've been trying to present my data from my database using PHP. My while
I'm trying to feed data from SQL database to FLEX, using php script in
I'm trying to make a site that loads values from a database and then
I am trying to display from a MySQL Database using PHP and Ajax but
I'm trying to get data from database, and echo them on the page using
trying to get my head around using Lambda expressions to fetch data from my
I'm trying to write a program that will fetch data from a SQL Server
I have been trying to display image from database using php but i am

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.