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.
Related
I would like to process JSON data started with an object that I don't need.
Here you have the URL:
http://datos.santander.es/api/rest/datasets/callejero_calles.json?items=819
I have been trying to adapt next code, but I don't know how to avoid the first object (summary) and take the second one (resources).
If I want to take one by one all the data inside from each object of "resources" (for example, showing "nombre-calle", "tipo-via"...).
package leerjson;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class LeerJSON {
public static void main(String[] args) throws ParseException {
JSONParser parser = new JSONParser();
try {
URL oracle = new URL("http://datos.santander.es/api/rest/datasets/callejero_calles.json?items=819"); // URL to Parse
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
JSONArray a = (JSONArray) parser.parse(inputLine);
// Loop through each item
for (Object o : a) {
JSONObject datos = (JSONObject) o;
System.out.println(datos);
}
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
UPDATED:
Once seen Enra64's answer, I don't know how to use getJSONArray and getJSONObject because it is not a method. I have included json-simple-1.1.1.jar to my project, but it doesn't work. Thank you in advance! This is my new code:
URL oracle = new URL("http://datos.santander.es/api/rest/datasets/callejero_calles.json?items=819"); // URL to Parse
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine = in.readLine();
JSONObject a = (JSONObject) parser.parse(inputLine);
JSONArray resources = a.getJSONArray("resources");
for (int i = 0; i < resources.length(); i++) {
resources.getJSONObject(i);
}
Select the resources object as follows:
JSONObject a = (JSONObject) parser.parse(inputLine);
JSONArray resources = a.getJSONArray("resources");
And then loop through it:
for (int i = 0; i < resources.length(); i++) {
resources.getJSONObject(i);
}
I have in my java application some data that I want to send them to php using json to insert them later in database. the problem is with the json string.
Main.java
String args = "{\"nom\":\""+hostName+"\",\"host_name\":\""+hostName+"\", \"os_name\":\""+nameOS+"\",\"os_type\":\""+osType+"\",\"os_version\":\""+osVersion+"\"}";
Cpu.main(args);
Cpu.java
package webservice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Cpu {
public static void main(String args) {
try {
// make json string, try also hamburger
// send as http get request
URL url = new URL("http://localhost:8080/parc/index.php?order=" + args);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
PHPFILE.php
<?php
$order = $_GET["order"];
$obj = json_decode($order);
$nom = $obj->{"nom"};
$host_name = $obj->{"host_name"};
$os_name = $obj->{"os_name"};
$os_type = $obj->{"os_type"};
$os_version = $obj->{"os_version"};
echo $host_name;
echo json_last_error(); // 4 (JSON_ERROR_SYNTAX)
echo json_last_error_msg(); // unexpected character
$array = array("nom" => $nom, "host_name" => $host_name, "os_name" => $os_name, "os_type" => $os_type, "os_version" => $os_version);
echo json_encode($array);
?>
Now I think that the problem with the json string format wich is "args" because when I change the variables hostname, os_type... like this : (String json = "{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}";) (from a tutorial) it works normally.
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
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());
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 ?