Structure JSOUP content to JSON - java

I extract some data from a http site and now I want to format this using JSON . I watched some examples but i don't understand how to do that exactly .I want something like that :
{
Product name:"Samsung..."
{ review 1:"..."
review 2:"..."
}
}
Also my code don't iterate over all reviews, it stops at page 1 ,and there is 10 more pages and i want to get like 20-30 reviews at least. There is what i did until now :
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupMain {
private static int nr = 1;
public static void main(String[] args) {
Document document = null;
try {
document = Jsoup.connect("http://www.emag.ro/telefon-mobil-samsung- galaxy-j5-2016-dual-sim-16gb-4g-gold-sm-j510fzdurom/pd/DQD3B7BBM/").get();
String title = document.title();
System.out.println("Title: " + title);
Elements review = document.select("div.product-review-body");
for (Element rev : review) {
System.out.println("Review : " + nr + " :" + rev.text() + "\n");
nr++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

See this answer for a similar question - Parsing table html with Jsoup In your case it might look like this:
JSONObject root = new JSONObject();
JSONArray reviews = new JSONArray();
root.put("name", "Product Name");
for (int i=0; i < elements.size(); i++){
JSONObject review = new JSONObject();
review.put("review"+i , elements.get(i).text());
reviews.add(review);
}
root.put("reviews", reviews);
The JSON output for root would look like this:
{
"name":"Product Name",
"reviews":[
{
"review0":"ok"
},
{
"review1":"awesome"
}
]
}

I want to make this code to show info exactly like that:
{ Reviews:
{ review 1: "..."}
{ review 2: "..."}
................
................
}
In present my code is printing info's like that: "Reviews":[[{"Review 0":"Foarte multumit Un telefon foarte bun. Ce m-a impresionat este camera foto care face niste poze foarte bune"},{"R............}
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupMain {
private static int nr = 1;
public static void main(String[] args) {
Document document = null;
try {
document = Jsoup.connect("http://www.emag.ro/telefon-mobil-samsung-galaxy-j5-2016-dual-sim-16gb-4g-gold-sm-j510fzdurom/pd/DQD3B7BBM/").get();
String title = document.title();
System.out.println("Product name : " + title);
Elements review = document.select("div.product-review-body");
JSONObject mainObject = new JSONObject();
JSONObject root = new JSONObject();
JSONArray list = new JSONArray();
root.put("Product name", title);
for (int i = 0; i < review.size(); i++) {
JSONObject revieww = new JSONObject();
revieww.put("Review " + i, review.get(i).text());
list.put(revieww);
}
mainObject.accumulate("Reviews", list);
System.out.println(mainObject);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Replace your code from Elements review = document.select("div.product-review-body");... to ... System.out.println(mainObject); with the following code snippet.
Elements review = document.select("div[class=product-review-body]");
JSONObject parent = new JSONObject();
parent.put("Product name", title);
JSONArray reviews = new JSONArray();
int i = 1;
for (Element rev : review) {
reviews.put(new JSONObject().put("review" + i, rev.text()));
i++;
}
parent.put("Reviews", reviews);
System.out.println(parent);
Hope this helps.

Related

JSONObject["data"] not found in Test (Junit5) [duplicate]

I want to read this JSON file with java using json library
"ListeCar": [
{
"id": "R",
"size": "2",
"Orientation": "Horizontal",
"Position": {
"Row": "2",
"Column": "0"
}
}
This is my java code :
package rushhour;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.*;
public class JsonClass {
public static void main(String[] args) throws IOException, JSONException {
try{
JSONObject obj = new JSONObject(new FileReader("C:\\Users\\Nuno\\Desktop\\School\\clg-g41326\\RushHourJson.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray Liste = obj.getJSONArray("ListeCar");
String listeCar = Liste.getJSONObject(0).getString("id");
for (int i = 0; i <Liste.length(); i++) {
String id = Liste.getJSONObject(i).getString("id");
System.out.println(id);
String size = Liste.getJSONObject(i).getString("size");
System.out.println(size);
String Orientation = Liste.getJSONObject(i).getString("Orientation");
System.out.println(Orientation);
String Position = Liste.getJSONObject(i).getString("Position");
System.out.println(Position);
}
}catch(JSONException e){
e.printStackTrace();
}
}
}
I'm doing this in netbeans and it's kind a my first time using Json !
I want just to do a system.out from this little json code. I don't know why he's not finding the file that i put in the new JSONObjet ...
{
"ListeCar":[
{
"id":"R",
"size":"2",
"Orientation":"Horizontal",
"Position":{
"Row":"2",
"Column":"0"
}
}]
}
try placing this in your .json file
your json is not valid... try placing it in this site to check for it's validity.... http://json.parser.online.fr/
And the code for the correct output....
public static void main(String[] args) throws IOException, JSONException, ParseException {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("/home/Desktop/temp.json"));
JSONObject objJsonObject = new JSONObject(obj.toString());
System.out.println(objJsonObject);
JSONArray Liste = objJsonObject.getJSONArray("ListeCar");
String listeCar = Liste.getJSONObject(0).getString("id");
for (int i = 0; i < Liste.length(); i++) {
String id = Liste.getJSONObject(i).getString("id");
System.out.println(id);
String size = Liste.getJSONObject(i).getString("size");
System.out.println(size);
String Orientation = Liste.getJSONObject(i).getString("Orientation");
System.out.println(Orientation);
String Position = Liste.getJSONObject(i).getJSONObject("Position").toString();
System.out.println(Position);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
You forgot to parse json... which is done in the above code.... a link about the tutorial on how to do this is as follows:: http://crunchify.com/how-to-read-json-object-from-file-in-java/

Why json parser program doesn't find array?

Pls help me to understand why this java program doesn't find array from json file. I didn't find similar type json file via google so pls educate me.
Error:
C:\temp\example.json
org.json.JSONException: JSONObject["result"] not found.
at org.json.JSONObject.get(JSONObject.java:572)
at org.json.JSONObject.getJSONArray(JSONObject.java:765)
at JsonParsingMachine.main(JsonParsingMachine.java:17)
.java content:
import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.json.*;
public class JsonParsingMachine {
public static void main(String[] args) {
String tiedosto = "C:/temp/example.json";
System.out.println(Paths.get(tiedosto));
try {
String contents = new String((Files.readAllBytes(Paths.get(tiedosto))));
JSONObject o = new JSONObject(contents);
JSONArray res = o.getJSONArray("result");
for (int i = 0; i < res.length(); i++) {
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
json file (example.json)
{
"quoteResponse" : {
"result" : [ {
"language" : "en-US",
"region" : "US",
"quoteType" : "EQUITY",
"quoteSourceName" : "Nasdaq Real Time Price",
"triggerable" : true
} ]
}
}
result array is inside quoteResponse JSONObject. You need to do this instead:
JSONObject o = new JSONObject(contents);
JSONObject quoteResponse = o.getJSONObject("quoteResponse");
JSONArray res = quoteResponse.getJSONArray("result");

A space in JSON value causes "unexpected token END OF FILE at position 11" exception when parsing in Java

When I use the parser from org.json.simple.parser.* I get an exception whenever one of the values in JSON contains a space. For example:
{"name":"Adam"}
would parse correctly, but
{"name":"Ad am"}
would cause "unexpected token END OF FILE at position 11" exception
Here is the code that I use to convert a JSON string into a JSONObject.
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringJSON);
Try to get through this below example and each value contains space except integer one and giving this example just because of you don't have shared your source code.
JSON File(personal_detail.json):
{
"name":"arif mustafa",
"age":26,
"address":["district is Korba","state is Chhattisgarh","country is India"]
}
Java source to read the JSON file format:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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;
public class JSONExample {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("src/resources/personal_detail.json"));
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject + "\n");
String name = (String) jsonObject.get("name");
System.out.println("name : " + name);
long age = (Long) jsonObject.get("age");
System.out.println("age : " + age);
//get Object loop array
JSONArray address = (JSONArray) jsonObject.get("address");
System.out.println("address is : ");
Iterator<String> iterator = address.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Convert JSONObject into string and long return null

When jsonobject is converted to String or long it returns null. Why?
My JSON file:
{
"memberships": [
{
"project": {
"id": 30483134480107,
"name": "Asana Integrations"
},
"section": null
}
]
}
And my code:
package jsontest;
import java.beans.Statement;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class MoreComplexJson {
private static final String filePath = "C:\\jsonTestFile.json";
public static void main(String[] args) {
try {
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray memberships = (JSONArray) jsonObject.get("memberships");
for (int z = 0; z < memberships.size(); z++) {
Iterator m = memberships.iterator();
// take each value from the json array separately
while (m.hasNext()) {
JSONObject innerObj = (JSONObject) m.next();
Long id = (Long) innerObj.get("id");
String name = (String) innerObj.get("name");
System.out.println("id " + id + " with name " + name);
}
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
System.out.println(ex + "");
}
catch (IOException ex) {
ex.printStackTrace();
ex.printStackTrace();
System.out.println(ex + "");
}
catch (ParseException ex) {
ex.printStackTrace();
ex.printStackTrace();
System.out.println(ex + "");
}
catch (NullPointerException ex) {
ex.printStackTrace();
ex.printStackTrace();
System.out.println(ex + "");
}
}
}
The output:
id null with name null
id and name belongs to the project JSONObject so get those two values using the project JSONObject
Try this for loop
for (int z = 0; z < memberships.size(); z++) {
JSONObject m = (JSONObject) memberships.get(z);
JSONObject innerObj = (JSONObject) m.get("project");
// If you want section
String section = (String) m.get("section");
System.out.println("section " + section);
Long id = (Long) innerObj.get("id");
String name = (String) innerObj.get("name");
System.out.println("id " + id + " with name " + name);
}
The problem is that when you are trying to get id and name you're not taking it from project but from object that contains project. There should be:
JSONObject innerObj = (JsonObject) ((JSONObject) m.next()).get("project)";
This kind of code can get pretty ugly pretty fast. Instead you could use a higher order parser, such as Jackson. Then your code can be much cleaner and you don't have to worry about digging into the conversion of each piece of JSON.

How to retrieve all users in my domain

I created one domain in that domain i created nearly 500 user accounts.I want to retrieve all the users in my domain .so that i use the following coding to retrieve all the users in my domain.But in that coding i displayed only first 100 users.And also it display total user entries 100.I dont know what problem in this coding.
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
/**
* This is a test template
*/
public class AppsProvisioning {
public static void main(String[] args) {
try {
// Create a new Apps Provisioning service
UserService myService = new UserService("My Application");
myService.setUserCredentials(admin,password);
// Get a list of all entries
URL metafeedUrl = new URL("https://www.google.com/a/feeds/"+domain+"/user/2.0/");
System.out.println("Getting user entries...\n");
UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);
List<UserEntry> entries = resultFeed.getEntries();
for(int i=0; i<entries.size(); i++) {
UserEntry entry = entries.get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
System.out.println("\nTotal Entries: "+entries.size());
}
catch(AuthenticationException e) {
e.printStackTrace();
}
catch(MalformedURLException e) {
e.printStackTrace();
}
catch(ServiceException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
what problem in this coding?
The list of users is returned in an atom feed. This is a paged feed, with a maximum of 100 entries per page. If there are more entries in the feed then there will be an atom:link element with a rel="next" attribute, pointing at the next page. You need to keep following these links until there are no more 'next' pages.
See: http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#Results_Pagination
The code will look something like:
URL metafeedUrl = new URL("https://www.google.com/a/feeds/"+domain+"/user/2.0/");
System.out.println("Getting user entries...\n");
List<UserEntry> entries = new ArrayList<UserEntry>();
while (metafeedUrl != null) {
// Fetch page
System.out.println("Fetching page...\n");
UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);
entries.addAll(resultFeed.getEntries());
// Check for next page
Link nextLink = resultFeed.getNextLink();
if (nextLink == null) {
metafeedUrl = null;
} else {
metafeedUrl = nextLink.getHref();
}
}
// Handle results
for(int i=0; i<entries.size(); i++) {
UserEntry entry = entries.get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
System.out.println("\nTotal Entries: "+entries.size());

Categories