2

I have Json like:

{"created_at":"2014","name":"solicitice"}
{"created_at":"2015","name":"hior"}

.....

I am trying below code to get values of all variables: ....

JSONObject jsonObj = new JSONObject(sb.toString());
for (int i = 0; i < jsonObj.length(); i++) {
 System.out.println(jsonObj.get("created_at"));
}

but it is printing only few lines of same value . Also ArrayList is not effective in this case.

asked Oct 26, 2015 at 10:04
7
  • are you getting some error? also provide an example where its failing.. Commented Oct 26, 2015 at 10:07
  • 1
    The provided sample is not valid JSON. Should be enclosed in [] and comma separated. Commented Oct 26, 2015 at 10:10
  • @X.L.Ant Maybe their input is not a JSON array but rather a list of JSON objects separated by a newline? Commented Oct 26, 2015 at 10:28
  • @flashdrive2049 Why not, but in this case, JsonParser won't be able to parse that at once AFAIK. The objects will have to be splitted into an array before getting parsed one by one. Commented Oct 26, 2015 at 10:30
  • @X.L.Ant That's why I suggested using a StringReader wrapped with a BufferedReader in my answer. Commented Oct 26, 2015 at 16:42

4 Answers 4

3

If your input as a string is:

"{\"created_at\":\"2014\",\"name\":\"solicitice\"}\n{\"created_at\":\"2015\",\"name\":\"hior\"}"

and it is guaranteed that there is exactly one object per line, you can use a StringReader combined with a BufferedReader to read those objects and then read their values. For example like this:

String input = "...";
try (StringReader sr = new StringReader(input); BufferedReader in = new BufferedReader(sr)) {
 String line;
 while ((line = in.readLine()) != null) {
 JSONObject object = new JSONObject(line);
 System.out.println(object.get("created_at"));
 }
} catch (IOException e) {
 // this should not happen but let's log it anyway
 e.printStackTrace();
}

This will read every line in input, transform it into a JSONObject and print its created_at field to the standard output.

answered Oct 26, 2015 at 10:25
Sign up to request clarification or add additional context in comments.

Comments

2

Because you should define it as json array, not json object. Try:

JSONArray jsonArr = new JSONArray(sb.toString());
for (int i = 0; i < jsonArr.length(); i++) {
 System.out.println(jsonArr.get(i).get("created_at"));
}
mezzodrinker
99810 silver badges28 bronze badges
answered Oct 26, 2015 at 10:08

3 Comments

If the two lines that @Aquarius24 added to their question are their actual input, this would cause an error. A JSON array is defined by an opening bracket [, followed by an arbitrary amount of values separated by comma , and a closing bracket ].
it depends on the json library you used. Btw, what was the library?
I am using import org.json.JSONArray; import org.json.JSONObject;
1

Few Corrections,

1.) Json String is not correct, should be in form of array

2.) Use JsonArray to pass, then loop, get JsonObject, then get values through keys.

public static void main(String[] args) throws JSONException {
 String s = "[{\"created_at\":\"2014\",\"name\":\"solicitice\"},{\"created_at\":\"2015\",\"name\":\"hior\"}]";
 JSONArray jsonArr = new JSONArray(s);
 for (int i = 0; i < jsonArr.length(); i++) {
 JSONObject jsonObj = (JSONObject) jsonArr.get(i);
 System.out.println(jsonObj.get("created_at"));
 }
 }

output

2014
2015
answered Oct 26, 2015 at 10:12

Comments

1
 StringBuffer jsonStr = new StringBuffer("[{\"created_at\":\"2014\",\"name\":\"solicitice\"},{\"created_at\":\"2015\",\"name\":\"hior\"}]");
 JsonParser jsonParser = new JsonParser();
 JsonArray jsonArray = new JsonArray();
 jsonArray = jsonParser.parse(jsonStr.toString()).getAsJsonArray();
 System.out.println("JSON Array: " + jsonArray);
 for(JsonElement json: jsonArray){
 System.out.println("JSON ELEMENT: " + json);
 System.out.println("NAME: " + json.getAsJsonObject().get("name"));
 System.out.println("CREATED_AT: " + json.getAsJsonObject().get("created_at"));
 }
answered Oct 26, 2015 at 10:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.