My Json Data:
{'ID':1,'FirstName':'x','LastName':'y','Company':'x','EMail':'x','PhoneNo':'x'}
My Java Code:
public static void main(String[] args) throws IOException {
String json = getJSON().substring(getJSON().indexOf("[")+1,getJSON().indexOf("]"));
Users user = new Gson().fromJson(json, Users.class);
WriteLine("["+user.getID()+"]"+" "+user.getFirstName()+" "+user.getLastName()+" "+user.getCompany()+" "+user.getEMail()+" "+user.getPhoneNo());
}
static void WriteLine(String text){
System.out.print(text);
}
static String getJSON() throws IOException
{
URL url = new URL("http://localhost:51679/api/User");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sBuilder = new StringBuilder();
String line;
while((line = reader.readLine()) != null){
sBuilder.append(line);
}
reader.close();
connection.disconnect();
return sBuilder.toString();
}
But my json data As these become:
{'ID':1,'FirstName':'x','LastName':'x','Company':'x','EMail':'x','PhoneNo':'x'},{'ID':2,'FirstName':'y','LastName':'y','Company':'y','EMail':'x','PhoneNo':'y'}
I have a error: Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 136
Can you help me? Sorry for my bad english :(
Read the error message carefully. That input is not valid JSON, which is exactly what the message tells you. Strings and keys must be surrounded with double quotes, not single quotes:
{"ID":1,"FirstName":"x","LastName":"y","Company":"x","EMail":"x","PhoneNo":"x"}
A simple check with a JSON validator would tell you the same. Alternately – again, as the error message conveniently tells you – you could set the reader to be lenient with JsonReader.setLenient(true), to hopefully accept malformed JSON as input.
Related
I'm trying to parse the first instance of "actualEPS" from the following JSON file:
{"symbol":"AAPL","earnings":[{"actualEPS":2.34,"consensusEPS":2.17,"estimatedEPS":2.17,"announceTime":"AMC","numberOfEstimates":10,"EPSSurpriseDollar":0.17,"EPSReportDate":"2018-07-31","fiscalPeriod":"Q3 2018","fiscalEndDate":"2018-06-30","yearAgo":1.67,"yearAgoChangePercent":0.40119760479041916,"estimatedChangePercent":0.29940119760479045,"symbolId":11},{"actualEPS":2.73,"consensusEPS":2.69,...}
Here is the method currently. I know I'm getting data from the target as I can Sysout the String "inputLine" and see the full file. I'm having trouble parsing from that point. I have installed and imported the org.JSON library.
public static void getData(String s) throws Exception {
String urlBase = new String(theWebSiteImGettingDataFrom)
URL targetURL = new URL(urlBase);
URLConnection yc = targetURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
} // end while
in.close();
JSONObject obj = new JSONObject(inputLine);
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
} // end getData method
I'm getting a Null Pointer Exception in the stack trace:
at org.json.JSONTokener.<init>(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:357)
at code.URLConnectionReader.getData(URLConnectionReader.java:39)
How do I parse the data for the first instance of "actualEPS" and only the first instance?
EDIT
JSONObject obj = new JSONObject(inputLine);
JSONArray earningsArray = obj.getJSONArray("earnings");
JSONObject firstEPS = earningsArray.getJSONObject(0);
double eps = firstEPS.getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
First of all, as I have no idea how big your json is and as you only want to parse a specific part of the json itself, I would recommend you to use JsonReader.class instead of JsonObject.class.
Short difference:
JsonObject parses the whole json into RAM and needs to be smaller than 1 MB.
JsonReader uses a streaming approach, which allows you to handle big jsons more efficiently.
Secondly, if you know that you ALWAYS just need the first instance of your json you could simply shorten your jsonString itself before parsing it (e.g. substring, etc.).
Now to your code:
public static void getData(String s) throws Exception {
String urlBase = new String(theWebSiteImGettingDataFrom); // Semicolon!
URL targetURL = new URL(urlBase);
URLConnection yc = targetURL.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
} // end while
in.close();
System.out.println("My json: "+inputLine); //TODO: What is your output here?
JSONObject obj = new JSONObject(inputLine);
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
System.out.println("This is the first instance of EPS: " + eps);
} // end getData method
Please comment, as I can't tell if you get the json from your server.
EDIT:
Your error is in this line:
double eps = obj.getJSONObject("earnings").getDouble("actualEPS");
In short it should be like this:
double eps = obj.getJSONArray("earnings").getJSONObject(0).getDouble("actualEPS");
But, why? Your attribute earnings returns an JSONArray (Which means that you have multiple rows with indizes). So, instead of just requesting "earnings" as a JSONObject, you should rather use it as a JSONArray and then extract the first row (= .getJSONObject(0)). After extracting the first row you can actually use your double-Value.
I hope this works :)
2nd Edit:
Change that ..
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine); } // end while
to:
while ((inputLine += in.readLine()) != null) {
System.out.println(inputLine); } // end while
Your while loop keeps iterating until .readLine() returns null.
As null is the last iteration, you have only null in your variable.
As suggested you can solve that by simply changing the = to +=.
I hope we got it now. :)
I have file content which is currently as byte[] and I want to know it's mime-type / content-type header.
The issue is that, I saw many examples over the web to find it but only when the file is actually exists.
How can I get this info by only this byte[].
Most of the codes which I tested are like:
File f = new File("gumby.gif");
System.out.println("Mime Type: " + new MimetypesFileTypeMap().getContentType(f));
EDIT 1:
This code gave me a wrong result which is: application/octet-stream
Is there a way to get this info without creating a file ?
EDIT 2:
I tried this code:
public static void main(String[] args) throws Exception{
Tika tika = new Tika();
byte[] content = readFile().getBytes();
System.out.println(tika.detect(content));
}
private static String readFile() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("c:\\pic.jpg"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
return everything;
} finally {
br.close();
}
}
And it's always returns application/octet-stream, is it because i'm using byte[] as parameter?
When I debug the code below, in the "Variables" view, both response and this.response show the entire 1,779 lines of streamed input from http://www.google.com. If, however, I want to output this.response to the console with System.out.println(this.response.toString();, it only outputs the last few lines.
Initially I thought it was a limitation of the String class. To test this I copied the 1,779 lines and assigned them to a test String variable. When I output that test String variable, it output all 1,779 lines to the console just fine.
What am I missing where both this.respponse and response show the entire document, but when I go to output either of them, I only get the last few lines?
public class ClassC {
private String url = "http://www.google.com";
private URL URL;
private HttpURLConnection con;
private String response;
public static void main(String[] args) {
new ClassC();
}
public ClassC() {
try {
URL = new URL(url);
con = (HttpURLConnection) URL.openConnection();
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
System.out.println(response.toString());
}
rd.close();
this.response = response.toString();
System.out.println(this.response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Try \n instead of \r.
'\r' is a carriage return - it returns the caret to the start of the line, but doesn't start a new line, effectively overwriting the current line (or parts of it).
e.g. System.out.println("abcde\rfghi") results in fghie.
There is no limitation in Java that I am aware of with printing content. The issue could be with your console. Are you using Eclipse or some other IDE to develop and run the application? If so - then yes by default the older lines will be truncated on the Eclipse Run Console by default. Also the line is redundent,
this.response = response.toString();
I am using this code to read data from a webpage :
public class ReadLatex {
public static void main(String[] args) throws IOException {
String urltext = "http://chart.apis.google.com/chart?cht=tx&chl=1+2%20\frac{3}{4}";
URL url = new URL(urltext);
BufferedReader in = new BufferedReader(new InputStreamReader(url
.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
// Process each line.
System.out.println(inputLine);
}
in.close();
}
}
The webpage gives the image for a latex code in the URL.
I am getting this exception:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://chart.apis.google.com/chart?
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at ReadLatex.main(ReadLatex.java:11)
Can anyone tell why I am having this problem and what should be the solution for this?
Try escaping with something like org.apache.commons.lang.StringEscapeUtils
Your problem is that you are using a \ (backslash) in a string which in Java is a escape character. To get an actual \ you need to have two of them in your string. So:
Wanted text: part1\part2
you need to have
String theString = "part1\\part2";
So you actually want
String urltext = "http://chart.apis.google.com/chart?cht=tx&chl=1+2%20\\frac{3}{4}";
Also, when you succeed with your request you get back an image (png) which should not be read with a reader which will try to interpret the bytes as characters using some encoding and this will break the image data. Instead, use the input stream and write the content (bytes) to a file.
A simple example without error handling
public static void main(String[] args) throws IOException {
String urltext = "http://chart.apis.google.com/chart?cht=tx&chl=1+2%20\\frac{3}{4}";
URL url = new URL(urltext);
InputStream in = url.openStream();
FileOutputStream out = new FileOutputStream("TheImage.png");
byte[] buffer = new byte[8*1024];
int readSize;
while ( (readSize = in.read(buffer)) != -1) {
out.write(buffer, 0, readSize);
}
out.close();
in.close();
}
I think you should consider escaping the backslash in the URL. I Java, the backslash must be escaped in a String
It should become
String urltext =
"http://chart.apis.google.com/chart?cht=tx&chl=1+2%20\\frac{3}{4}";
This was for the pure java start.
It seems that this url works with my browser but, as suggested in the other answers, I think it should be better to also escape all the special characters such as backslashes, laces...
I'm using Bing's auto suggest feature to auto suggest me terms given a query. You can find the tool here: http://api.bing.com/osjson.aspx?query=pe as you can see it's returning a strange format that isn't quite JSON. Is this a specific standard different to JSON? I've attempted parsing it as JSON using...
InputStream i = new URL(url).openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(i, Charset.forName("UTF-8")));
JSONObject json = new JSONObject(readAll(reader));
but I get the error A JSONObject text must begin with '{' found:" at 2 [character 3 line 1]
readAll =
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();
}
Your example is valid JSON:
["pe",["people","people search","petsmart","petco","petfinder","pep boys","people finder","people of walmart"]]
It is not object, it is array, which contains string at the first position and another array at the second. So try parse as JSONArray, not as JSONObject.
A JSON Object starts with a { and ends with a }, which a JSONObject class was designed to parse.
A JSON Array starts with a [ and ends with a ], which a JSONArray class was designed to parse.
I hope this helps.