Get JSON String in java with org.json - java

[{"post":{"titel":"Glee","interpret":"Bran Van 3000","jahr":"1997","id":"5"}},{"post":{"titel":"Goodbye Country (Hello Nightclub)","interpret":"Groove Armada","jahr":"2001","id":"4"}},{"post":{"titel":"Beauty","interpret":"Ryuichi Sakamoto","jahr":"1990","id":"1"}}]
The above string is what i have in json and now what i want to do is to just get the values of "post" and "titel" as in the above string what i am having 3 values of post which makes sense to me. But, now what i want is to print all the 3 values of post and each value in the post string.
JSONArray jsonArray = new JSONArray(jsonString);
columns = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
columns[i]=jsonObject.getString("post");
}
above code returns me 3 values of post. WHat i can do if i want to print values of post and the each value of column in post?

I would do something like this (I don't use the columns array just to print, and this code is not tested):
JSONArray jsonArray = new JSONArray(jsonString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject postObj = jsonObject.getJSONObject("post");
for (String i : postObj.keys()) {
System.out.println("Key: " + i + "; Value: " + postObj.getString(i));
}
}
That is, process each element of the Object returned by the "post" object of each array element.

Related

want to store multiple JSON objects to jsonArray and get the result as a single string

Here is my code for storing multiple JSON objects into an array and then I want to convert the array into a single string.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
for(int i= 0; i<= array.length(); i++) {
array.put(json);
message = array.toJSONString();
}
I want to get the output as a string and store it in a message variable.
String message =null;
JSONObject json = new JSONObject();
json.put("name","student");
json.put("name", "Atal");
json.put("name","Rachit");
JSONArray array = new JSONArray();
//for(int i= 0; i<= array.length(); i++) {
// array.put(json);
// message = array.toJSONString();
//}
array.put(json); //There is a JSON,just put it into the array
message = array.toJSONString();
I was replacing "name" every time I was calling json.put()
String message =null;
JSONObject json = new JSONObject();
json.put("name","Atal");
json.put("class", "10");
json.put("Roll","1035");
JSONArray array = new JSONArray();
array.put(json);
message = array.toJSONString();

How to Get String From Parse JSON without String Id in Android Studio

How do I get a string from Parse JSON with data like below?
My Data JSON :
[
[
"John Gorman",
"3344764667200003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Andy William",
"3403032311690003",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Thomas Endry",
"3408932311690078",
"Student",
"2018-08-09 08:21:30.807"
],
[
"Robet Calm",
"3403077711690890",
"Student",
"2018-08-09 08:21:30.807"
]
]
I use the code below, then to get the string what I have to do, I really don't understand, if there is help, I really need to thank you.
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString("What should I write here");
System.out.println(c.getString(""));
}
I want to get the string from Parse JSON that I have
How do I get a string for listview, for example:
John Gorman
3344764667200003
Student
Andy William
3403032311690003
Student
Thomas Endry
3408932311690078
Student
Robet Calm
3403077711690890
Student
A couple of things are wrong with the posted code. See my comments inline:
JSONObject object = new JSONObject(result);
^^^^^^^^^^^^^^^^^^^^^^
the content of result is not an object, but an array!
JSONArray jsonArray = object.getJSONArray("");
^^
a proper JSON document had better not have empty keys!
//JSONArray jsonArray = new JSONArray(result);
^^^^^^^^^^^^^^^^^^^^^
this was actually going in the right direction!
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
^^^^^^^^^^^^^^^^^^^^^^^^^^
the i-th element of the array is not an object, but an array!
Fixing the issues above:
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray subArray = jsonArray.getJSONArray(i);
for (int j = 0; j < subArray.length(); j++) {
System.out.println(subArray.getString(j));
}
System.out.println();
}
You are receiving a JSONArray not object so use the below code.
for(int i=0;i<jsonarray.length();i++){
JSONArray array=jsonarray.getJSONArray(i);
for(int j=0;j<array.length();j++){
Log.d(TAG,array.getString(j));
}
}
You can do something like this:
try {
JSONObject object = new JSONObject(result);
JSONArray jsonArray = object.getJSONArray("");
//JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String name = c.getString(0);
String id = c.getString(1);
String student = c.getString(2);
// and so on ... This index refers to the index number on the string inside json object
System.out.println(name);
}
Hope this answers your query.
Following method will construct a list of string from the given Json. You can pass that list in the adapter of the list view.
public List<String> getList (String result) throws JSONException {
List<String> list = new LinkedList<>();
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray arr = jsonArray.getJSONArray(i);
StringBuilder sb = new StringBuilder();
for(int j=0;j<arr.length(); j++) {
sb.append(arr.getString(j));
if(j<arr.length()-1) sb.append("\n");
}
list.add(sb.toString());
}
return list;
}

Java - retrieving JSON object values

i have a JSON object which looks something like this:
cb = {"content":[{"name":"abc"}{"name":"bcd"}{"name":"xyz"}...]}
and i have imported the JSON library as:
import org.json.JSONObject;
Now i want to use a for loop to retrieve the values of name in each of the sub objects...
for(int x = 10; x < numberOfSubElemtnts; x = x+1) {
//print out the name values
}
Output should be:
abc
bcd
xyz
How should I evaluate the length of the array and print out the name values?
You can iterate the array like this:
String content = "{\"content\":[{\"name\":\"abc\"},{\"name\":\"bcd\"},{\"name\":\"xyz\"}]}";
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("content");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("name"));
}
JSONParser parser = new JSONParser();
String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
try{
Object obj = parser.parse(s);
JSONArray array = (JSONArray)obj;
for(int i=0;i<array.length();i++{
System.out.println(array.get(i).getString("name");
}
}catch(ParseException pe){
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
Also with this example youll have to change the format of your string to fit the one I have in mine

How to access key:values in JSON using Java with no array names

Lets say I have a JSON array like this
[{"a":"1", "b":"2"}, {"c":"3", "d":"4"}]
I'm trying to get it in Java like this
JSONArray array = new JSONArray(responseBody); //resposeBody is the JSON array
for (int i = 0; i < array.length(); i++) {
String arr = array.get(i).toString(); //Trying to get each array like this
JSONArray json = new JSONArray(arr);
}
At the line of JSONArray json = new JSONArray(arr);I get the error
A JSONArray text must start with '['
How do I access the values?
EDIT: I mean how do I get each array and their values
Your JSONArray is an array of JSONObjects.
You can try
JSONArray array = new JSONArray(responseBody);
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
}
Now you can further parse the individual JSONObjects.
Just do :
JSONObject myJsonObject = new JSONObject(jsonString);
In array:
JSONObject myjObject = myJsonArray.getJSONObject(i);

Use values (integers) returned in JSONArray

Im having issues with being able to get the indivisual responses from this JSON Array. Im trying to write some code that will allow me to use the vaules indivisually, as well as how to combine them if i needed too. These responses are always integers.
Java
JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");
for (int i = 0; i < data_array.length(); i++) {
JSONObject prod = data_array.getJSONObject(i);
Log.d("Logging Response", prod);
}
Json
[5652,8388,8388,7537,8843,2039,8235,0,12220]
I'd like to figure out how i can add them together and return the calculated result, as well as how to use them individually. such as (prod + prod + prod) etc...
JSONArray data = jsonObj.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
Integer prod = (Integer) data.get(i);
System.out.println("Prod " + prod);
// loop and add it to array or arraylist
}
You have the jsonarray as the response
ArrayList<Integer> jsonvalue=new ArrayList<Integer>();
for (int i = 0; i < data_array.length(); i++) {
int i= data_array.getInt(i);
jsonValue.add(i)
}
To put that json array data in to a new json object, try this:
JSONObject jsonObj = new JSONObject(jsonStr);
data_array = jsonObj.getJSONArray("data");
JSONObject prod;
for (int i = 0; i < data_array.length(); i++) {
prod.put(i,data_array.getJSONObject(i));
Log.d("Logging Response", prod);
}
JSONArray jsonObj = new JSONArray(jsonStr);
for (int i = 0; i < jsonObj.length(); i++) {
System.out.println(jsonObj.getString(i));
}

Categories