Java JSON

Ive got a Java app that uses the google search api

https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="

Open in new window


The results are attached, im trying to work out how to prase the data and use it?


{"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483","url":"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483","visibleUrl":"www.become.com","cacheUrl":"http://www.google.com/search?q\u003dcache:_rEm1_LH-O0J:www.become.com","title":"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews \u003cb\u003e...\u003c/b\u003e","titleNoFormatting":"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews ...","content":"\u003cb\u003e...\u003c/b\u003e Terry Moses Basket Gift Set, Sage The new Organic Waffle \u003cb\u003eWeave\u003c/b\u003e Moses   Basket, \u003cb\u003e...\u003c/b\u003e compare moses baskets -\u003cb\u003efiletype:pdf\u003c/b\u003e -filetype:ps -filetype:dwf -filetype:  kml \u003cb\u003e...\u003c/b\u003e"}],"cursor":{"resultCount":"1","pages":[{"start":"0","label":1}],"estimatedResultCount":"1","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dinurl:basket+weaving+FileType:Pdf","searchResultTime":"0.09"}}, "responseDetails": null, "responseStatus": 200}

Open in new window

k3eperAsked:
Who is Participating?
 
CEHJCommented:
0
 
for_yanCommented:



Look at this question - it has examples of  parsing JSON
http://www.experts-exchange.com/Programming/Languages/Java/Q_27068225.html
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
k3eperAuthor Commented:
yeah ive installed GSON and JSON.lib but I have no idea what to do with them.

This is the code that produces the JSON string.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class Main extends JFrame implements ActionListener,ItemListener
{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	String[] FileType ={"Pdf" , "Doc" , "txt" , "xls"};

	JPanel pnl ;
	JTextField txt1 ;
	JButton btn1 ;
	JTextArea txtArea ;
	JCheckBox chkpdf = new JCheckBox(FileType[0]);
	JCheckBox chkdoc = new JCheckBox(FileType[1]);
	JCheckBox chktxt = new JCheckBox(FileType[2]);
	JCheckBox chkxls = new JCheckBox(FileType[3]);
	JComboBox box;
    public String searchString;
	public Main()
		{
		super("Swing Window");
		pnl = new JPanel();
        JPanel pnl1 = new JPanel();
        pnl1.setLayout(new FlowLayout());
        JPanel pnl2 = new JPanel();
        txt1 = new JTextField(20);
        box    = new JComboBox(FileType);

        btn1 = new JButton("Search");
        txtArea = new JTextArea(20,30);
        txtArea.setLineWrap(true);

        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());



        pnl.setLayout(new BorderLayout());
        pnl2.add(box);
		pnl1.add(btn1);
		pnl1.add(txt1);
        pnl.add(pnl1,BorderLayout.NORTH);
		pnl2.add(new JScrollPane(txtArea));
        pnl.add(pnl2, BorderLayout.CENTER);
		btn1.addActionListener(this);
	

        container.add(pnl);
        setSize(500, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
		}

	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(btn1)) {
		    String searchParam = txt1.getText().trim();
		    if (searchParam.trim().length() == 0) {
			String txtCheck = "Enter Query String";


			txtArea.setText(txtCheck);
             return;
		    }
			else

		    	searchString = "inurl:" + txt1.getText()+ "%20FileType:" + box.getSelectedItem() ;
				 System.out.println(searchString);//check output is correct	
				 try {
					 Search SearchObject = new Search();
					 SearchObject.searchGoog(searchString);
					    txtArea.setText(SearchObject.getLine());
					    return;
					} catch (Exception ex) {
					    ex.printStackTrace();

					}

		    }
			}



	public static void main (String[] args)
	{
	new Main();
	}



	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		
	}


}

Open in new window

import java.io.*;
import java.net.*;


public class Search {
public String line;
    
    public void searchGoog(String searchString) throws IOException {
        
	URL url = new URL("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + searchString);
	URLConnection connection = url.openConnection();
        
	StringBuilder builder = new StringBuilder();
	BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String buff = null;
	while((buff = reader.readLine()) != null) {
	builder.append(buff);
        
	}
	
System.out.println(builder.toString());
        line = builder.toString();

    }
 public String getLine() {
        return line;
    
    }

    public void setLine(String line) {
        this.line = line; 
    }    
}

Open in new window

0
 
for_yanCommented:

You can download thwe JSON library form here:

http://www.roseindia.net/tutorials/json/JSONLibraries.zip

and this is an example of the code to get contents for JSON into HAshMaps, etc.

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import java.util.*;

public class DeserializeJSON {




public static ArrayList getArrList(String jsonString){
ArrayList al = new ArrayList();
JSONObject json = JSONObject.fromObject(jsonString);
    Iterator it = json.keys();
    String ss = (String) it.next();

JSONArray jar1 = json.getJSONArray(ss);
for(int jja=0; jja<jar1.size();jja++){
    HashMap m = new HashMap();
JSONObject j1 = jar1.getJSONObject(jja);
    Iterator it1 = j1.keys();
    while(it1.hasNext()){
        String s1 = (String) it1.next();
        String val = j1.optString(s1);
         m.put(s1,val);
    }
 al.add(m);
}
return al;
}

public static void main(String [] args){
    String jsonString = "{\"hashmaps\":[{\"username\":\"abc\",\"userid\":\"1234\"},{\"username\":\"def\",\"userid\":\"5678\"}]}";
      ArrayList ar = getArrList(jsonString);
    for(int j=0; j<ar.size(); j++){
        HashMap m = (HashMap)ar.get(j);
        System.out.println("HashMap No. " + j);
        Set<String> s = m.keySet();
        for(String s0: s){
            System.out.println(s0 + ":" + (String)m.get(s0));
        }

    }
   

    
}

}

Open in new window

0
 
for_yanCommented:
But you ned the opposite way - to parse the JSON string - look at the code I psoed above.
0
 
for_yanCommented:
Well, it is not that straightforward but JSON is usually combined of come combination fo JSON Objects and JSON array's,
so you need to analyze it and parse accordingly. Requires a little bit of work, but with the help of these libraries it is doable -
I deriliazed a few of them when I neede several times.
0
 
for_yanCommented:
I am not sure you can derialize JSON easiliy for any JSON string in general but if you want
to derialize particular string on a particular site which has known structure - then after some work it is possible.
That's at least was my experience with JSON.
0
 
CEHJCommented:
How you treat the data returned to you by google is really dependent on your use case. What do your users need?
0
 
for_yanCommented:
This is the way you parse the JSON string you
posted in your question (see output):

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import java.util.*;

public class DeserializeJSON {




public static void analyzeJSON(String jsonString){
ArrayList al = new ArrayList();
JSONObject json = JSONObject.fromObject(jsonString);


    Iterator it = json.keys();
    String ss = null;
    while(it.hasNext()){
     ss = (String) it.next();
         System.out.println("1: " + ss);
        String sval = json.optString(ss);
      //  System.out.println("1,value: " + sval);
        if(!sval.startsWith("{") && !sval.startsWith("[") )continue;
        JSONObject json1 =  JSONObject.fromObject(sval);
         //JSONObject json1 =  json.optJSONObject(sval);
        if(json1 == null)continue;
        if(json1.isEmpty())continue;
        Iterator it1 = json1.keys();
        while(it1.hasNext()) {
            String ss1 = (String) it1.next();
              System.out.println("2: " + ss1);
              String sval1 = json1.optString(ss1);
         //    System.out.println("2,value: " + sval1);
              JSONArray jar = json1.optJSONArray(ss1);
             JSONObject json3 = json1.optJSONObject(ss1);

         //   System.out.println("for " + ss1 + " : " + jar);
            if(jar != null){
             for (int j=0; j<jar.size(); j++){
                // System.out.println("array for " + ss1 + " num " + j + ": "  + jar.get(j));

                 JSONObject json2 = JSONObject.fromObject(jar.get(j));
                  Iterator it2 = json2.keys();
                 while(it2.hasNext()){
                     String ss2 = (String)it2.next();
                     String sval2 = json2.optString(ss2);
                     System.out.println("3: (key, value): " + ss2 + " , " + sval2);

                 }


             }

            } else {

                Iterator it3 = json3.keys();
                  while(it3.hasNext()){
                     String ss3 = (String)it3.next();
                     String sval3 = json3.optString(ss3);
                     System.out.println("4: (key, value): " + ss3 + " , " + sval3);
                      JSONArray jar2 = json3.optJSONArray(ss3);
                      if(jar2 != null){
     for (int j=0; j<jar2.size(); j++){
                 System.out.println("array for " + ss3 + " num " + j + ": "  + jar2.get(j));

                 JSONObject json4 = JSONObject.fromObject(jar2.get(j));
                  Iterator it4 = json4.keys();
                 while(it4.hasNext()){
                     String ss4 = (String)it4.next();
                     String sval4 = json4.optString(ss4);
                     System.out.println("5: (key, value): " + ss4 + " , " + sval4);

                 }


             }


                      }

                 }


            }

             


        }

        //JSONObject json1 = JSONObject.fromObject(ss);
      //  JSONArray jar1 = json.getJSONArray(ss);
       // System.out.println(jar1.size());


    }
     /*
JSONArray jar1 = json.getJSONArray(ss);
for(int jja=0; jja<jar1.size();jja++){
    HashMap m = new HashMap();
JSONObject j1 = jar1.getJSONObject(jja);
    Iterator it1 = j1.keys();
    while(it1.hasNext()){
        String s1 = (String) it1.next();
        String val = j1.optString(s1);
         m.put(s1,val);
    }
 al.add(m);
}
    */
//return al;
}

public static void main(String [] args){

  String jsonString =  "{\"responseData\": {\"results\":[{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483\",\"url\":\"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483\"," +
          "\"visibleUrl\":\"www.become.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:_rEm1_LH-O0J:www.become.com\",\"title\":\"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews \u003cb\u003e...\u003c/b\u003e\"," +
          "\"titleNoFormatting\":\"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews ...\",\"content\":\"\u003cb\u003e...\u003c/b\u003e Terry Moses Basket Gift Set, Sage The new Organic Waffle \u003cb\u003eWeave\u003c/b\u003e Moses   Basket, \u003cb\u003e...\u003c/b\u003e compare moses baskets -\u003cb\u003efiletype:pdf\u003c/b\u003e -filetype:ps " +
          "-filetype:dwf -filetype:  kml \u003cb\u003e...\u003c/b\u003e\"}],\"cursor\":{\"resultCount\":\"1\",\"pages\":[{\"start\":\"0\",\"label\":1}],\"estimatedResultCount\":\"1\",\"currentPageIndex\":0,\"moreResultsUrl\":\"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dinurl:basket+weaving+FileType:Pdf\"," +
          "\"searchResultTime\":\"0.09\"}}, \"responseDetails\": null, \"responseStatus\": 200}";
    //String jsonString = "{\"hashmaps\":[{\"username\":\"abc\",\"userid\":\"1234\"},{\"username\":\"def\",\"userid\":\"5678\"}]}";
      analyzeJSON(jsonString);
        /*
    for(int j=0; j<ar.size(); j++){
        HashMap m = (HashMap)ar.get(j);
        System.out.println("HashMap No. " + j);
        Set<String> s = m.keySet();
        for(String s0: s){
            System.out.println(s0 + ":" + (String)m.get(s0));
        }


    }

    */



}

}

Open in new window


Output:
1: responseData
2: results
3: (key, value): GsearchResultClass , GwebSearch
3: (key, value): unescapedUrl , http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483
3: (key, value): url , http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483
3: (key, value): visibleUrl , www.become.com
3: (key, value): cacheUrl , http://www.google.com/search?q=cache:_rEm1_LH-O0J:www.become.com
3: (key, value): title , Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews <b>...</b>
3: (key, value): titleNoFormatting , Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews ...
3: (key, value): content , <b>...</b> Terry Moses Basket Gift Set, Sage The new Organic Waffle <b>Weave</b> Moses   Basket, <b>...</b> compare moses baskets -<b>filetype:pdf</b> -filetype:ps -filetype:dwf -filetype:  kml <b>...</b>
2: cursor
4: (key, value): resultCount , 1
4: (key, value): pages , [{"start":"0","label":1}]
array for pages num 0: {"start":"0","label":1}
5: (key, value): start , 0
5: (key, value): label , 1
4: (key, value): estimatedResultCount , 1
4: (key, value): currentPageIndex , 0
4: (key, value): moreResultsUrl , http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=inurl:basket+weaving+FileType:Pdf
4: (key, value): searchResultTime , 0.09
1: responseDetails
1: responseStatus

Open in new window

0
 
k3eperAuthor Commented:
thanks for the responses, the idea is to return the restults and display the title as actionable allowing to download the file if you want to do so.
0
 
for_yanCommented:
then you parse in the way I sohowed and then store title vs. the  URL of the file in the HashMap
and then ddispaly list iof titles - as user clickes onn the title you go to the address of the file and download it
0
 
k3eperAuthor Commented:
I want to GREP everything but the title and the actual URL so I can retrieve the file. The code about is only formatting the data into something more readable and passing the data into some arrays. Is that correct?
0
 
for_yanCommented:
Wait a moment , I'll return you the HashMap with the required data
0
 
for_yanCommented:
This retrievs list of titles and hashmap of titles - URLs  (se at the bottom of the output - after all thisi debugging output).

Of course this is specific to eeaxctly this struictur of JSON
If JSON has different structure you need to modify
the program

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import java.util.*;

public class DeserializeJSON {


    static HashMap<String,String> titlesToURLs;
   static  ArrayList<String>titles;

public static void analyzeJSON(String jsonString){
ArrayList al = new ArrayList();
JSONObject json = JSONObject.fromObject(jsonString);


    Iterator it = json.keys();
    String ss = null;
    while(it.hasNext()){
     ss = (String) it.next();
         System.out.println("1: " + ss);
        String sval = json.optString(ss);
      //  System.out.println("1,value: " + sval);
        if(!sval.startsWith("{") && !sval.startsWith("[") )continue;
        JSONObject json1 =  JSONObject.fromObject(sval);
         //JSONObject json1 =  json.optJSONObject(sval);
        if(json1 == null)continue;
        if(json1.isEmpty())continue;
        Iterator it1 = json1.keys();
        while(it1.hasNext()) {
            String ss1 = (String) it1.next();
              System.out.println("2: " + ss1);
              String sval1 = json1.optString(ss1);
         //    System.out.println("2,value: " + sval1);
              JSONArray jar = json1.optJSONArray(ss1);
             JSONObject json3 = json1.optJSONObject(ss1);

         //   System.out.println("for " + ss1 + " : " + jar);
            if(jar != null){
             for (int j=0; j<jar.size(); j++){
                // System.out.println("array for " + ss1 + " num " + j + ": "  + jar.get(j));

                 JSONObject json2 = JSONObject.fromObject(jar.get(j));
                  Iterator it2 = json2.keys();
                 String URLString = null;
                 String titleString = null;
                 while(it2.hasNext()){
                     String ss2 = (String)it2.next();
                     String sval2 = json2.optString(ss2);
                     System.out.println("3: (key, value): " + ss2 + " , " + sval2);
                     if(ss2.equals("unescapedUrl")) { URLString = sval2;}

                     if(ss2.equals("title")) { titleString = sval2;
                     titles.add(titleString);
                         titlesToURLs.put(titleString,URLString );
                         // titleString = null;
                        // URLString = null;


                     }

                     

                     }

                 }




            } else {

                Iterator it3 = json3.keys();
                  while(it3.hasNext()){
                     String ss3 = (String)it3.next();
                     String sval3 = json3.optString(ss3);
                     System.out.println("4: (key, value): " + ss3 + " , " + sval3);
                      JSONArray jar2 = json3.optJSONArray(ss3);
                      if(jar2 != null){
     for (int j=0; j<jar2.size(); j++){
                 System.out.println("array for " + ss3 + " num " + j + ": "  + jar2.get(j));

                 JSONObject json4 = JSONObject.fromObject(jar2.get(j));
                  Iterator it4 = json4.keys();
                 while(it4.hasNext()){
                     String ss4 = (String)it4.next();
                     String sval4 = json4.optString(ss4);
                     System.out.println("5: (key, value): " + ss4 + " , " + sval4);

                 }


             }


                      }

                 }


            }

             


        }

    
    }
  }

public static void main(String [] args){
    titles = new ArrayList<String>();
        titlesToURLs = new HashMap<String, String>();
    

  String jsonString =  "{\"responseData\": {\"results\":[{\"GsearchResultClass\":\"GwebSearch\",\"unescapedUrl\":\"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483\",\"url\":\"http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483\"," +
          "\"visibleUrl\":\"www.become.com\",\"cacheUrl\":\"http://www.google.com/search?q\u003dcache:_rEm1_LH-O0J:www.become.com\",\"title\":\"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews \u003cb\u003e...\u003c/b\u003e\"," +
          "\"titleNoFormatting\":\"Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews ...\",\"content\":\"\u003cb\u003e...\u003c/b\u003e Terry Moses Basket Gift Set, Sage The new Organic Waffle \u003cb\u003eWeave\u003c/b\u003e Moses   Basket, \u003cb\u003e...\u003c/b\u003e compare moses baskets -\u003cb\u003efiletype:pdf\u003c/b\u003e -filetype:ps " +
          "-filetype:dwf -filetype:  kml \u003cb\u003e...\u003c/b\u003e\"}],\"cursor\":{\"resultCount\":\"1\",\"pages\":[{\"start\":\"0\",\"label\":1}],\"estimatedResultCount\":\"1\",\"currentPageIndex\":0,\"moreResultsUrl\":\"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003dinurl:basket+weaving+FileType:Pdf\"," +
          "\"searchResultTime\":\"0.09\"}}, \"responseDetails\": null, \"responseStatus\": 200}";
    //String jsonString = "{\"hashmaps\":[{\"username\":\"abc\",\"userid\":\"1234\"},{\"username\":\"def\",\"userid\":\"5678\"}]}";
      analyzeJSON(jsonString);

      System.out.println("\n\n\nResults\n\n\n");
    System.out.println("Arraylist of names: " + titles);
    System.out.println("HashMap of names-URLs: " + titlesToURLs);


      


}

}

Open in new window


Output:
1: responseData
2: results
3: (key, value): GsearchResultClass , GwebSearch
3: (key, value): unescapedUrl , http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483
3: (key, value): url , http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483
3: (key, value): visibleUrl , www.become.com
3: (key, value): cacheUrl , http://www.google.com/search?q=cache:_rEm1_LH-O0J:www.become.com
3: (key, value): title , Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews <b>...</b>
3: (key, value): titleNoFormatting , Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews ...
3: (key, value): content , <b>...</b> Terry Moses Basket Gift Set, Sage The new Organic Waffle <b>Weave</b> Moses   Basket, <b>...</b> compare moses baskets -<b>filetype:pdf</b> -filetype:ps -filetype:dwf -filetype:  kml <b>...</b>
2: cursor
4: (key, value): resultCount , 1
4: (key, value): pages , [{"start":"0","label":1}]
array for pages num 0: {"start":"0","label":1}
5: (key, value): start , 0
5: (key, value): label , 1
4: (key, value): estimatedResultCount , 1
4: (key, value): currentPageIndex , 0
4: (key, value): moreResultsUrl , http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=inurl:basket+weaving+FileType:Pdf
4: (key, value): searchResultTime , 0.09
1: responseDetails
1: responseStatus



Results



Arraylist of names: [Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews <b>...</b>]
HashMap of names-URLs: {Beautiful Baby Blue Dot Moses Basket Gift - Prices, Reviews <b>...</b>=http://www.become.com/beautiful-baby-blue-dot-moses-basket-gift--compare-prices--sc1207271483}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.