Monthly Archives: February 2014

Parsing JSON data using Google GSON library in Eclipse

Setup:
1. Download gson library
2. Add as external jar from your eclipse project

JSON String:
{
“status”:”200″,
“error_message”:”Success”,
“item”:
[{“ID”:”18″, “Name”:”Name A”} ,
{“ID”:”19″, “Name”:”Name B”} ,
{“ID”:”20″, “Name”:”Name C”} ,
{“ID”:”21″, “Name”:”Name D”}]
}

Java class to implement the parsing

public class gsonexample {

	public gsonexample(){}

	public void parseJson(String url){
		try{
			Reader reader = new InputStreamReader(new URL("url").openStream()); //Read the json output
			Gson gson = new GsonBuilder().create();
			DataObject obj = gson.fromJson(reader, DataObject.class);
			System.out.println(obj);
		}catch(Exception e){
			System.out.println(e);
		}
	}

	private class DataObject{ //This class should match your json object structure
		private int status;
		private String error_message;
		private List<Item> item; // This is for the inner array
		@Override
	    public String toString() {
	        return status + " - " + error_message+ " (" + item + ")";
	    }
	}

	private class Item{ //This is the inner array class
		public int ID;
		public String Name;
		@Override
	    public String toString() {
	        return ID + " - " + Name +"\n";
	    }
	}
}
Advertisements

Create simple JSON data from PHP from mysql query

function was not tested, minor modification will be needed. For this in Java

Example output:
{
“status”:”200″,
“error_message”:””,
“item”:[
{“ID”:”18″, “Name”:”Item A”} ,
{“ID”:”19″, “Name”:”Item B”} ,
{“ID”:”20″, “Name”:”Item C”} ,
{“ID”:”21″, “Name”:”Item D”}
]
}

</pre>
function constructJson($query){
 $json_str = "";
 $row_count = 0;
 if($total=mysql_num_rows($query)){
 $json_str = constructJsonHead().constructJsonStatus(200,"")."[";
 $json_item = "";
 while($data=mysql_fetch_assoc($query)){
 $count = 0;
 if(count($data)>1)
 $json_item .= constructJsonHead();

foreach($data as $key => $value){
 if(count($data)>1)
 $json_item .= "\"$key\":\"$value\"";
 else
 $json_item .= "\"$value\"";

$count++;
 if($count < count($data)) $json_item .= ",\n"; } $row_count++; if(count($data) > 1)
 $json_item .= constructJsonTail()."\n";

if($row_count < $total)
 $json_item .= ",\n";

}
 }else{
 $json_str .= constructJsonHead().constructJsonStatus(201,"No Item")."[]".constructJsonTail();
 }
 return $json_str.$json_item."]".constructJsonTail();
}

function constructJsonStatus($response_code,$msg){
 return "\"status\":\"$response_code\",\"error_message\":\"$msg\",\"item\":";
}

function constructJsonHead(){ return "{";}
function constructJsonTail(){ return "}";}
<pre>

Simple Fibonacci Series


private void fibonacci(List list,int arg){
 if(arg==0){
 list.add(0);
 }else{
 if(list.isEmpty()){
 list.add(0);
 list.add(1);
 }
 int temp = (int)list.get(list.size()-1) + (int)list.get(list.size()-2);
 if(arg>=temp){
 list.add(temp);
 fibonacci(list,arg);
 }
 }
 }

Check integer pallindrome without using build-in utilities

</pre>
public static boolean pallindrome(int arg){
int remainder = 0;
int newArg = arg;
int result = 0;
while(newArg!=0){
remainder = newArg % 10;
newArg = newArg / 10;
result = result*10 + remainder;
}
if(result == arg){
return true;
}else{
return false;
}
}
<pre>