Parse json response ?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.xml.sax.InputSource;
public class JsonSimpleExample {
public static String processURL(String address) {
StringBuffer res= new StringBuffer();
try{
URL url = new URL(address);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
// System.out.println(inputLine);
res.append(inputLine);
}
br.close();
}catch(MalformedURLException me){
me.getMessage();
}catch(Exception e){
e.getMessage();
}
return res.toString();
}
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
String url = "Enter your url here";
String stream = processURL(url);
System.out.println(stream);
Object obj = parser.parse(stream);
JSONObject jsonObject = (JSONObject) obj;
// loop array
JSONArray items = (JSONArray) jsonObject.get("nameofobject");
Iterator<JSONObject> iterator = items.iterator();
int i=0;
while (iterator.hasNext()) {
System.out.println(i++);
JSONObject jObj= iterator.next();
String title = (String) jObj.get("title");
System.out.println(title);
String link = (String) jObj.get("link");
System.out.println(link);
String snippet = (String) jObj.get("snippet");
System.out.println(snippet);
// System.out.println(iterator.next());
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.xml.sax.InputSource;
public class JsonSimpleExample {
public static String processURL(String address) {
StringBuffer res= new StringBuffer();
try{
URL url = new URL(address);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
// System.out.println(inputLine);
res.append(inputLine);
}
br.close();
}catch(MalformedURLException me){
me.getMessage();
}catch(Exception e){
e.getMessage();
}
return res.toString();
}
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
String url = "Enter your url here";
String stream = processURL(url);
System.out.println(stream);
Object obj = parser.parse(stream);
JSONObject jsonObject = (JSONObject) obj;
// loop array
JSONArray items = (JSONArray) jsonObject.get("nameofobject");
Iterator<JSONObject> iterator = items.iterator();
int i=0;
while (iterator.hasNext()) {
System.out.println(i++);
JSONObject jObj= iterator.next();
String title = (String) jObj.get("title");
System.out.println(title);
String link = (String) jObj.get("link");
System.out.println(link);
String snippet = (String) jObj.get("snippet");
System.out.println(snippet);
// System.out.println(iterator.next());
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment