I'm trying to parse the following url:
http://api.crossref.org/works?rows=2
When I parse it through Gson, I got some records but somehow some others stay null.
Here is my code:
BufferedReader in = new BufferedReader(new InputStreamReader(url_tdm.openStream(), "UTF-8"));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = in.read(chars)) != -1)
buffer.append(chars, 0, read);
String jsonLine = buffer.toString();
JsonReader reader = new JsonReader(new StringReader(jsonLine));
reader.setLenient(true); // this is for Malformed json
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create();
Crossref answer = gson.fromJson(reader, Crossref.class );
List<Items> ao = answer.message.items;
public class Crossref {
public Message message;}
public class Message {
public List<Items> items;}
public class Items {
public List<String> containerTitle;
public List<String> ISSN;
public String publisher;
public List<String> title;
public String DOI;
public String type;}
So as a result of my code above, I can get container-title, publisher and title values. But ISSN and DOIs are null.
I used a FieldNamingPolicy because "container-title" contains a dash and I could not name my field like that in java (so I wrote it as camel case containerTitle).
I am not sure if this affects DOI and ISSN records which are upper case or is it something totally different?
The best way to fix something like this is to use a gson custom deserializer
I suggest that you read this other question to see a good exemple: How do I write a custom JSON deserializer for Gson?
And you can find some other greats exemples and explanations here
Related
I have a trouble finding a way how to parse JSONArray.
It looks like this:
[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
I know how to parse it if the JSON was written differently (In other words, if I had json object returned instead of an array of objects).
But it's all I have and have to go with it.
*EDIT: It is a valid json. I made an iPhone app using this json, now I need to do it for Android and cannot figure it out.
There are a lot of examples out there, but they are all JSONObject related. I need something for JSONArray.
Can somebody please give me some hint, or a tutorial or an example?
Much appreciated !
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}
I'll just give a little Jackson example:
First create a data holder which has the fields from JSON string
// imports
// ...
#JsonIgnoreProperties(ignoreUnknown = true)
public class MyDataHolder {
#JsonProperty("name")
public String mName;
#JsonProperty("url")
public String mUrl;
}
And parse list of MyDataHolders
String jsonString = // your json
ObjectMapper mapper = new ObjectMapper();
List<MyDataHolder> list = mapper.readValue(jsonString,
new TypeReference<ArrayList<MyDataHolder>>() {});
Using list items
String firstName = list.get(0).mName;
String secondName = list.get(1).mName;
public static void main(String[] args) throws JSONException {
String str = "[{\"name\":\"name1\",\"url\":\"url1\"},{\"name\":\"name2\",\"url\":\"url2\"}]";
JSONArray jsonarray = new JSONArray(str);
for(int i=0; i<jsonarray.length(); i++){
JSONObject obj = jsonarray.getJSONObject(i);
String name = obj.getString("name");
String url = obj.getString("url");
System.out.println(name);
System.out.println(url);
}
}
Output:
name1
url1
name2
url2
Create a class to hold the objects.
public class Person{
private String name;
private String url;
//Get & Set methods for each field
}
Then deserialize as follows:
Gson gson = new Gson();
Person[] person = gson.fromJson(input, Person[].class); //input is your String
Reference Article: http://blog.patrickbaumann.com/2011/11/gson-array-deserialization/
In this example there are several objects inside one json array. That is,
This is the json array: [{"name":"name1","url":"url1"},{"name":"name2","url":"url2"},...]
This is one object: {"name":"name1","url":"url1"}
Assuming that you have got the result to a String variable called jSonResultString:
JSONArray arr = new JSONArray(jSonResultString);
//loop through each object
for (int i=0; i<arr.length(); i++){
JSONObject jsonProductObject = arr.getJSONObject(i);
String name = jsonProductObject.getString("name");
String url = jsonProductObject.getString("url");
}
public class CustomerInfo
{
#SerializedName("customerid")
public String customerid;
#SerializedName("picture")
public String picture;
#SerializedName("location")
public String location;
public CustomerInfo()
{}
}
And when you get the result; parse like this
List<CustomerInfo> customers = null;
customers = (List<CustomerInfo>)gson.fromJson(result, new TypeToken<List<CustomerInfo>>() {}.getType());
A few great suggestions are already mentioned.
Using GSON is really handy indeed, and to make life even easier you can try this website
It's called jsonschema2pojo and does exactly that:
You give it your json and it generates a java object that can paste in your project.
You can select GSON to annotate your variables, so extracting the object from your json gets even easier!
My case
Load From Server Example..
int jsonLength = Integer.parseInt(jsonObject.getString("number_of_messages"));
if (jsonLength != 1) {
for (int i = 0; i < jsonLength; i++) {
JSONArray jsonArray = new JSONArray(jsonObject.getString("messages"));
JSONObject resJson = (JSONObject) jsonArray.get(i);
//addItem(resJson.getString("message"), resJson.getString("name"), resJson.getString("created_at"));
}
Create a POJO Java Class for the objects in the list like so:
class NameUrlClass{
private String name;
private String url;
//Constructor
public NameUrlClass(String name,String url){
this.name = name;
this.url = url;
}
}
Now simply create a List of NameUrlClass and initialize it to an ArrayList like so:
List<NameUrlClass> obj = new ArrayList<NameUrlClass>;
You can use store the JSON array in this object
obj = JSONArray;//[{"name":"name1","url":"url1"}{"name":"name2","url":"url2"},...]
Old post I know, but unless I've misunderstood the question, this should do the trick:
s = '[{"name":"name1","url":"url1"},{"name":"name2","url":"url2"}]';
eval("array=" + s);
for (var i = 0; i < array.length; i++) {
for (var index in array[i]) {
alert(array[i][index]);
}
}
URL url = new URL("your URL");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
//setting the json string
String finalJson = buffer.toString();
//this is your string get the pattern from buffer.
JSONArray jsonarray = new JSONArray(finalJson);
Working with StringBuilder came across a problem. I need to put a StringBuilder in JSONArray, but when i did my string is shuffled. How can I fix this?
My line before:
[{"id":"iprod","name":"soup","categoryId":"categoryId123","categoryName":"soup","imageUrl":"c/image","price":5.0,"weight":123.0,"ingredients":["chicken","rice"],"nutrition":{"energy":{"kilojoules":123,"kilocalories":123},"fat":123,"carbohydrate":123,"protein":123},"currency":"123"}]
My line after:
[{"nutrition":{"protein":123,"fat":123,"carbohydrate":123,"energy":{"kilojoules":123,"kilocalories":123}},"price":5,"imageUrl":"c/image","name":"soup","weight":123,"ingredients":["chicken","rice"],"currency":"123","id":"iprod","categoryName":"soup","categoryId":"categoryId123"}]
Code:
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
System.out.println(sb.toString());
JSONArray responseObject = new JSONArray(sb.toString());
System.out.println(responseObject.get(0));
Your String is JSONObject not JSONArray.
From the JSON specification at http://www.json.org/
An object is an unordered set of
name/value pairs
JSON libraries are free to rearrange the order of the elements as they see fit. GSON, a Java library developed by Google for handling JSON. And it will save the order for you:
This is the code:
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class Test {
public static void main(String[] args) {
Gson gson = new Gson();
String line = "{\"id\":\"iprod\",\"name\":\"soup\",\"categoryId\":\"categoryId123\",\"categoryName\":\"soup\",\"imageUrl\":\"c/image\",\"price\":5.0,\"weight\":123.0,\"ingredients\":[\"chicken\",\"rice\"],\"nutrition\":{\"energy\":{\"kilojoules\":123,\"kilocalories\":123},\"fat\":123,\"carbohydrate\":123,\"protein\":123},\"currency\":\"123\"}\n";
System.out.println(line);
JsonObject responseObject = gson.fromJson(line, JsonObject.class);
System.out.println(responseObject.keySet());
}
}
I am new to JSON data format and java programming language; hence, I cannot find a valid answer. Actually, I have to read this API https://www.doviz.com/api/v1/currencies/all/latest, and obtain some important contents from this API. Hence, I decided to use google's GSON class, and I wrote this code.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
import com.google.gson.Gson;
public class Main {
public static void main( String[] args ) throws Exception{
String line = "";
String jsonString = "";
URL myUrl = new URL("https://www.doviz.com/api/v1/currencies/all/latest");
BufferedReader reader = new BufferedReader( new InputStreamReader(myUrl.openStream()) );
while( (line = reader.readLine()) != null ){
System.out.println(line);
jsonString += line;
}
reader.close();
jsonString = jsonString.substring(1, jsonString.length() - 1);
Gson gson = new Gson();
Currency json = gson.fromJson(jsonString, Currency.class);
}
}
public class Currency {
public double getSelling(){
return selling;
}
public double getBuyiing(){
return buying;
}
public String getCode(){
return code;
}
private double selling;
private transient long update_date;
private transient int currency;
private double buying;
private transient double change_rate;
private transient String name;
private transient String full_name;
private String code;
}
This code causes error, and as far as I guess, the main reason for the errors is that I do not put backslash in son string like this: "{\"brand\":\"Jeep\", \"doors\": 3}"
What I am wondering is why we need to put these backslash ?
There are 2 things to mention.
The " character is the String delimiter. A String starts at a " mark, and ends at the next one. (When initializing it explicitly, not using other variables) If you want to include " character in your String, you need to escape it like \" - so Java knows that it is not the end of the String, just a part of the content.
In JSON you should use single quotes ' - many libraries accept double quotes also, but it is not correct actually, and if any api complains about them, the API is right.
So your payload should look like {'brand': 'Jeep', 'doors': 3} I mean the other way around of course.
When you receive a JSON output from the api, you can directly use the output and can deserialize it. You don't need to have escape characters in the json string. They are required when you define the json string yourself. For example String s = "{\"current_user_url\"}"; because it is the compiler which forces you to escape it. But the json output you are getting as an API response is in a variable and if type of variable and the content you are assigning to it are same then compiler can't compain about that.
Now, I have used your code only but used the Github public API and I am able to deserialize the output json without any operation on the output string whatsoever like escaping the "" or changing "" to ''.
class Github {
private String current_user_url;
private String authorizations_url;
public String getCurrent_user_url() {
return current_user_url;
}
public void setCurrent_user_url(String current_user_url) {
this.current_user_url = current_user_url;
}
public String getAuthorizations_url() {
return authorizations_url;
}
public void setAuthorizations_url(String authorizations_url) {
this.authorizations_url = authorizations_url;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
String line = "";
String jsonString = "";
URL myUrl = new URL("https://api.github.com");
BufferedReader reader = new BufferedReader( new InputStreamReader(myUrl.openStream()) );
while( (line = reader.readLine()) != null ){
//System.out.println(line);
jsonString += line;
}
reader.close();
System.out.println(jsonString);
Gson g = new Gson();
Github gi = g.fromJson(jsonString, Github.class);
System.out.println(gi.getAuthorizations_url());
System.out.println(gi.getCurrent_user_url());
}
I also defined the json string myself and desrialized it using GSON. In this case while defining the json string I needed to escape the double quotes as shown below:
String s = "{\"current_user_url\":\"http://test.com/user\"}";
Gson g = new Gson();
Github gi = g.fromJson(s, Github.class);
System.out.println(gi.getCurrent_user_url());
Also, JSON strings should contain double quotes only and if you use single quotes then you may get an error.
I need to convert JSON string to Object[].
I tried with link1 and link2 and did not help me.
Code how i get JSON string:
public static String getListJsonString() {
String getListsUrl = BASE_URL + "lists";
String result = "";
try {
URL url = new URL(getListsUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + getAuthStringEnc());
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
This is example of my JSON:
And after i must fill ChomboBox on this way (this is example):
Object[] lists = getLists();
for(Object list : lists){
System.out.println("fill combobox");
}
You can use Gson, TypeToken and JSONObject, example:
final static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
final Type objectType = new TypeToken<Object>(){}.getType();
JSONObject obj = new JSONObject(result);
Vector<Object> lists = gson.fromJson(obj.toString(), objectType);
I suggest you should be using jackson lib. I linked a great quick tutorial that I find really clear and useful.
The idea behind jackson lib is that JSON format is a stringified Object format so you should be able to map it properly to java POJOs easily. (POJO = Plain old java object, which is an object with some fields, maybe some annotations on top of your fields and finally just getters and setters).
You can auto generate Jackson annotated POJOs classes from a json string using this link : http://www.jsonschema2pojo.org/ (just select "JSON" instead of "JSON SCHEMA", and maybe tune the other parameters depending on your need).
I can feel your pain sometimes it's hard to get a quick example up and running.
This is a very simple example how you can read your json document using Jackson library. You need a minimum of jackson-annotations-x.y.z.jar, jackson-core-x.y.z.jar and jackson-databind-x.y.z.jar files in a classpath.
https://github.com/FasterXML/jackson-databind/
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
public class TestJSON1 {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonObj;
String jsonStr = "{ \"list\": [ "
+ " {\"id\":\"1\", \"name\":\"test1\"}, "
+ " {\"id\":\"2\", \"name\":\"test2\"}, "
+ " {\"id\":\"3\", \"name\":\"test3\"} "
+ "]}";
jsonObj = mapper.readTree(jsonStr);
System.out.println(jsonObj.get("list"));
JsonNode jsonArr=jsonObj.get("list");
int count=jsonArr.size();
for (int idx=0; idx<count; idx++) {
jsonObj = jsonArr.get(idx);
System.out.println( jsonObj.get("id").asText()+"="+jsonObj.get("name").asText() );
}
}
}
I am struggling with some JSON parsing (with Google's GSON library).
Here is my minimal code example:
Gson gson = new Gson();
String line = "{\"method\":\"GET\",\"status\":\"200 - OK\"}";
AnalysisReport report = gson.fromJson(line, AnalysisReport.class); //this works
JsonReader reader = new JsonReader(new StringReader(line));
reader.setLenient(true); //accept malformed input
while (reader.hasNext()) {
report = gson.fromJson(reader, AnalysisReport.class); //this doesn't work
}
reader.close();
I get the following exception: (the string is 36 chars long)
Expected BEGIN_OBJECT but was END_DOCUMENT at line 1 column 37
My ReportAnalysis class looks like this:
public class AnalysisReport {
#SerializedName("method")
private String method;
#SerializedName("status")
private String status;
AnalysisReport(){
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getStatus() {
return status;
}
}
I do not see why my code is not working. I followed the exmaple from Google
= = = EDIT = = =
The reader.hasNext() method always returns true. It seams that the method does not consume any tokens from the stream. But somehow still moves to the end of the stream and failes reading objects.
The problem was indeed the loop (facepalm). This works perfectly fine:
JsonReader reader = new JsonReader(new StringReader(line));
reader.setLenient(true); //accept malformed input
report = gson.fromJson(reader, AnalysisReport.class);
Thanks
I just removed loop and its working fine .
String line = "{\"method\":\"GET\",\"status\":\"200 - OK\"}";
AnalysisReport report = new AnalysisReport();
Gson gson = new GsonBuilder().create();
JsonReader reader = new JsonReader(new StringReader(line));
reader.setLenient(true); //accept malformed input
report = gson.fromJson(line, AnalysisReport.class); //this doesn't work
reader.close();
System.out.println(report.toString());