Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

GSON- get json array object

csckidMar 3 2012 — edited Mar 5 2012
I am using json to transfer data from php to java. I am using google-GSON.
I wanted to sent entire table values in an arraylist and java will get data from the arraylist.
Basically, I need to help to check whether the procedure and coding pattern is ok or not.

I have one table fruit with two columns.
fruit(id, available)
<?php
	$data = array();
	$sql="select * from fruit";			
	$result=mysql_query( $sql);
	while( $r=mysql_fetch_array($result) ){
		$data[] = $r;
	}
	echo json_encode($data);
									 
?>
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());
		}
		
}
I get this exception
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Fruit
at Main2.decodeUsingGSON(Main2.java:53)
at Main2.getJsonFromUrlDB(Main2.java:38)
at Main2.main(Main2.java:14)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 2 2012
Added on Mar 3 2012
4 comments
1,512 views