How can I "put" the output generated, which looks to be valid JSON, into an actual JSON object?
According to this answer, gson requires that a class be defined. Surely there's a dynamic way to load or define a class? Or a generic type?
In XML a schema or DTD would be available. Can I dynamically load or find something like that for JSON, using gson?
code:
package net.bounceme.noagenda;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static java.lang.System.out;
public class NoAgenda {
public static void main(String[] args) throws MalformedURLException, IOException {
List<URL> urls = new ArrayList<>();
new NoAgenda().iterateURLs(urls);
}
private void iterateURLs(List<URL> urls) throws MalformedURLException, IOException {
urls.add(new URL("https://www.flickr.com/photos/"));
urls.add(new URL("http://www.javascriptkit.com/dhtmltutors/javascriptkit.json"));
urls.add(new URL("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json"));
for (URL url : urls) {
connect(url);
}
}
private void connect(URL url) throws IOException {
out.println(url);
String line = null;
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
while ((line = in.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
out.println(sb);
// HOW DO I TURN THIS INTO AN ACTUAL JSON??
}
}
wikipedia says:
Schema and metadata
JSON Schema
JSON Schema[20] specifies a JSON-based format to define the structure
of JSON data for validation, documentation, and interaction control. A
JSON Schema provides a contract for the JSON data required by a given
application, and how that data can be modified.
The arbitrary three URL's I'm working with:
https://www.flickr.com/photos/
http://www.javascriptkit.com/dhtmltutors/javascriptkit.json
http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json
You can use following
StringBuilder sb = new StringBuilder();
String line;
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
JSONObject json = new JSONObject(sb.toString());
Related
I'm trying to write a function in java that will basically copy and paste the html code from a div from a url. The data in question is from http://cdn.espn.com/sports/scores#completed however the data is not viewable when copied into my function using io streams. The data itsself is is viewable when I click inspect and control-f "completed-soccer" it shows up as but my code does not retrieve it at all. Here is the code i used.
package project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class DownloadPage {
public static void main(String[] args) throws IOException {
// Make a URL to the web page
URL url = new URL("http://cdn.espn.com/sports/scores#completed-soccer");
// Get the input stream through URL Connection
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
// read each line and write to System.out
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you can't reach the data with normal HTTP request, you have to use more complex library, as Selenium with a Webdriver.
This library allows you to really navigate in a webpage, execute javascript and inspect all the element.
You can find a lot of information and guides.
Try with this code
public static void main(String[] args) throws IOException {
URL url = new URL("http://cdn.espn.com/sports/scores#completed-soccer");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try
{
InputStream in = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
finally
{
urlConnection.disconnect();
}
}
I am trying to convert my Xml string into an json object. I am using the org.Json package. I have requested information from the wolfram alpha server. The server give back xml and i want to used the org.json package to convert it to json. The method I am trying to use is static which lies inside the XML class. I created a JSONObject method thinking that i would convert but i keep getting an error message.
Here is my code from my main method.
import java.net.*;
import java.io.*;
import org.json.*;
import java.net.URLConnection;
import java.util.Scanner;
import java.net.URL;
import javax.swing.*;
public class Test {
public static void main(String[] args)throws IOException, JSONException{//Beginning of class
// TODO Auto-generated method stub
String appID = "YWT4UP-Y9W7AREAHJ";
String search = "bird";
URL wolframData = new URL("http://api.wolframalpha.com/v2/query?input="+search+"&appid="+appID);
URLConnection connection = wolframData.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//reads in the data
String xmlDoc;
while((xmlDoc = in.readLine()) != null) //converts buffer reader to string
System.out.println(xmlDoc);
in.close();
JSONObject jsonDoc = (JSONObject) XML.toJSONObject(xmlDoc);
}//End of method
}//End of class
Here is the error message that is displayed when it reach the code that converts the xml to json:
Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:85)
at org.json.XMLTokener.<init>(XMLTokener.java:55)
at org.json.XML.toJSONObject(XML.java:329)
at Test.main(Test.java:31)
I think the answer should be ,
import java.net.*;
import java.io.*;
import org.json.*;
import java.net.URLConnection;
import java.util.Scanner;
import java.net.URL;
import javax.swing.*;
public class Test {
public static void main(String[] args)throws IOException, JSONException{//Beginning of class
// TODO Auto-generated method stub
String appID = "YWT4UP-Y9W7AREAHJ";
String search = "bird";
URL wolframData = new URL("http://api.wolframalpha.com/v2/query?input="+search+"&appid="+appID);
URLConnection connection = wolframData.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//reads in the data
String xmlDoc;
StringBuilder sb = new StringBuilder();
while((xmlDoc = in.readLine()) != null) { //converts buffer reader to string
System.out.println(xmlDoc);
sb.append(xmlDoc);
}
in.close();
JSONObject jsonDoc = (JSONObject) XML.toJSONObject(sb.toString());
}//End of method
}//End of class
Following code read whole data set from a website.But I want to read specific value record from a web site. As a example when I pass a word to a website it dynamically display weather the word which I entered was what kind of word.When I pass tree name this return biographical name.I want to read that biographical name.Not the all data set of a website.How should I change?
import java.net.*;
import java.io.*;
public class DataRecord {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}
So I am trying to retrieve JSON object from url.
And I cannot retrieve anything from url.
So to test why, I tried the same code on Spring application, and it works just fine.
It crashes on code below only on android environment.
Please help
package com.example.sandbox;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
}
Please check your internet connection and also check if you have added permission for INTERNET in your app's manifest that might be issue.
what error do you get exactly ?
Am working on J2ee web application and am trying to get the code JSON of a web page via it's url in purpose to parse it later. I've already tried the code below:
package so4308554;
import java.io.BufferedReader;
import java.io.IOException;``
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonReader {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("https://graph.facebook.com/19292868552");
System.out.println(json.toString());
System.out.println(json.get("id"));
}
}
But it works only with the given url if i put another one i get this exception:
"A JSONObject text must begin with '{' at character 1"
I think i can't get the Json content of web page if it is coded by html... any idea of what could be the causes??????? And how can i resolve my problem???
Are you trying to get json out of an html web page?
If the answer to the previous question is "No", validate the json you're trying to parse with something like this: http://jsonlint.com/, and check out the results.