JSON Parse to Array value not appended properly - java

I have an array String[] cruce_no.I am appending Values from JSON response to the array. JSON response is a,b,c .... I need to append value Select to the 0th position of Array 'cruce_no'.
I need result like this {Select,a,b,c...}. But I am getting like this {Select,b,c...}. My code is below:
JSONArray JA=new JSONArray(result);
JSONObject json= null;
cruce_no = new String[JA.length()];
name = new String[JA.length()];
cruce_no[0] = "Select";
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
cruce_no[i] = json.getString("
}
How can I solve this? Please help me.

Increase the size of the array by 1
cruce_no = new String[JA.length()+1];
cruce_no[0] = "Select";
Inside the forloop use this
cruce_no[i+1] = json.getString("YOUR_KEY");

You getting {Select,b,c...} instead of {Select,a,b,c...} because you add Select at 0th position so "a" replaced by "Select"
try like this
JSONArray JA=new JSONArray(result);
JSONObject json= null;
cruce_no = new String[JA.length()+1];
name = new String[JA.length()];
cruce_no[0] = "Select";
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
cruce_no[i+1] = json.getString("
}

Make array size new String[JA.length()+1] as you are going to add an extra element select in it. And then place select at 0th positon followed by rest of the elements. So 0th position should not be overwritten.

Related

How to extract json Objects from String Builder?

I have string builder of the form:
[{"abc" : ["something1", "something2"]}, {"def" : ["something3", "something4"]}]
How do I convert it so that I can perform get on the keys? For e.g.: object.get("abc") and i get the list back.
I looked around for solutions:
I tried doing:
JSONObject jsonObject = new JSONObject(sb.toString());
I get the following exception:
exception org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
I then tried taking a sub-string of the Json, like this:
JSONObject jsonObject = new JSONObject(sb.toString().substring(1, sb.toString().length()-1));
which gives back:
jsonObjwct {"Z-DwY2Ul":["Worried Blues","Worried Blues"]}
I get only the first object, i need all of them.
What modifications i can do to get the list back for a specific key?
It is a JSONArray of JSONObject's, so first convert it into JSONArray
JSONArray array = new JSONArray(sb.toString());
And then you can iterate by using for loop
for (int i = 0, size = array.length(); i++) {
JSONObject obj = array.optJSONObject(i); //{"abc" : ["something1", "something2"]}
}

iterate through an a json value containing an array to pick an image string

I am able to fetch values the db and pass it to a string array as shown
String[] strArrayCol = new String[6];
strArrayCol[4] = json_data.getString("images");
if you print the above you gets:
[{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png"}]
my attempt is on performing something like
ArrayList<String[]> imgCol...
imgCol.add(strArrayCol );
for (String [] val : imgCol){
System.out.println( val[4]);
}
let it prints
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png
please how can I achieve this
Based on your code and explanation, it seems you are JSON object "images" is the below string:
{images:[{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/wendzj5atiks45c3zw00.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/rg04t5vcp4yxwdew677n.png"},{"path":"http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/3yvy9f970vit2pxascv7.png"}])}
Which is a JSONArray for the value of images.., Your code should be like this to retrieve the value of path
I have removed extra backward slashes in the string.
JsonImplement.parseJson("{\"images\":[{\"path\":\"http://10.0.2.2:88//web/uploads/images/yi6ej6f524bepyujh49y.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/wendzj5atiks45c3zw00.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/rg04t5vcp4yxwdew677n.png\"},{\"path\":\"http://10.0.2.2:88//web/uploads/images/3yvy9f970vit2pxascv7.png\"}]}");
And this is the logic I wrote in another method...
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
JSONArray arr = (JSONArray) jsonObject.get("images");
for(int i=0;i<arr.size();i++)
{
JSONObject obj2 = (JSONObject) arr.get(i);
System.out.println(obj2.get("path"));
}
This will give you output of image path's
http://10.0.2.2:88//web/uploads/images/yi6ej6f524bepyujh49y.png
http://10.0.2.2:88//web/uploads/images/wendzj5atiks45c3zw00.png
http://10.0.2.2:88//web/uploads/images/rg04t5vcp4yxwdew677n.png
http://10.0.2.2:88//web/uploads/images/3yvy9f970vit2pxascv7.png
If you know that your image file names will always be formatted as follows:
{path}/{filename}
Then there is a simple technique to find the file name.
Find the last slash (/) character in the string.
The filename is everything that follows the last slash character.
For example:
http:\/\/10.0.2.2:88\/\/web\/uploads\/images\/yi6ej6f524bepyujh49y.png
has a "path" value of "http://10.0.2.2:88//web/uploads/images/"
and a "filename" value of "yi6ej6f524bepyujh49y.png"

Group multiple JSONObject into JSONArray

Hi I Have JSON response as
[
{"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
{"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
{"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
{"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},
{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},
{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},
{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},
{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},
{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},
{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},
{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"},
{"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
{"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
{"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
{"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},
{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},
{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},
{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},
{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},
{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},
{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},
{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}
]
Its all JSONObject but I want to group first 11 objects into array and next 11 entry into another array so response must be like:
[
[
{"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},
{"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},
{"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},
{"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}
],
[{"sr_no":"1","stn_name":"ALUVA","arrival_time":"5:59:30","distance":"1.81"},{"sr_no":"2","stn_name":"PULINCHUDU","arrival_time":"6:02:21","distance":"2.76"},{"sr_no":"3","stn_name":"COMPANYPADY","arrival_time":"6:04:19","distance":"3.76"},{"sr_no":"4","stn_name":"AMBATTUKAVU","arrival_time":"6:06:15","distance":"4.72"},{"sr_no":"5","stn_name":"MUTTOM","arrival_time":"6:08:11","distance":"8.14"},{"sr_no":"6","stn_name":"KALAMASSERY","arrival_time":"6:11:20","distance":"8.65"},{"sr_no":"7","stn_name":"CUSAT","arrival_time":"6:13:40","distance":"9.15"},{"sr_no":"8","stn_name":"PATHADIPALAM","arrival_time":"6:15:52","distance":"12.02"},{"sr_no":"9","stn_name":"EDAPPALLY","arrival_time":"6:18:12","distance":"12.55"},{"sr_no":"10","stn_name":"CHANGAMPUZHA PARK","arrival_time":"6:20:39","distance":"13.07"},{"sr_no":"11","stn_name":"PALARIVATTOM","arrival_time":"6:23:10","distance":"14.43"}]]
How to do this , because I have more than 900 record. In above I had showed only 2. Any suggestion will be appreciated, Thanks in advance. I am doing this for Android app
Parse the original JSON array string, and then add new JSON arrays containing up to 11 objects to a new array, adding each such array to another outer containing array:
// String input contains your string input JSON array
JSONArray jArray = new JSONArray(input); // your original JSON array
JSONArray outArray = new JSONArray(); // the output array of arrays
JSONArray temp = new JSONArray(); // holder for each internal array
for (int i=0; i < jArray.length(); i++) {
if (i % 11 == 0 && i > 0) {
outArray.put(temp);
temp = new JSONArray();
}
temp.put(jArray.getJSONObject(i));
}

Error when using JSONSimple to write a json file with for loop

I need to transform my data in a ArrayList to a JSON file, and I use JSON.simple. Everything is fine except one little thing that I want to get a result like ...... {source:0, target:1},{source:0, target:1},{source:0, target:2},{source:0, target:3} ...... but it returns ......{source:0, target:16},{source:0, target:16},{source:0, target:16}...... . My solution.size() is 17.
Here is my code:
JSONObject jsonObject = new JSONObject();
JSONObject jsonNodesObject = new JSONObject();
JSONObject jsonEdgesObject = new JSONObject();
JSONArray jsonNodesArray = new JSONArray();
JSONArray jsonEdgesArray = new JSONArray();
String instString = solutions.get(0).get("institution");
jsonNodesObject.put("name", instString);
// extract name and institution from ArrayList
for (int i = 0; i < solutions.size(); i++)
{
HashMap<String, String> eleHashMap= solutions.get(i);
String nameString = eleHashMap.get("name");
jsonNodesObject.put("name", nameString);
jsonNodesArray.add(jsonNodesObject);
jsonEdgesObject.put("source", 0);
jsonEdgesObject.put("target", i);
jsonEdgesArray.add(jsonEdgesObject);
}
jsonObject.put("nodes", jsonNodesArray);
jsonObject.put("edges", jsonEdgesArray);
System.out.println(jsonObject);
It seems that in every for loop, it refreshes the value of target: i of all my jsonEdgesArray.
Dose anyone know how to fix this? Thanks in advance!
As your iterating jsonNodesObject in for loop, same value will be put for jsonNodesObject.put("name", nameString); u have to initialize JSONObject jsonNodesObject = new JSONObject(); inside for loop

Mapping JSON to arrays/objects in Java - Android

I have a web system that returns a json string with the data that I need in an Android App. The string is below:
[
{"id":1,
"title":"Remove ViRuSeS",
"tagline":"Remove ViRuSeS",
"body":"Body",
"image":"index.jpg",
"steps":[
{"id":2,
"title":"Step 1",
"body":"Download Things!",
"position":1}
]
}
]
It should return an array of objects, with one of the object's items also being an array of items.
I am familiar with gson and have gotten this working in the past, but I always had to simplify my data down to just an object, which makes me end up have to make multiple calls to get the data.
Is there a good way to do this without having to map all of the possible values back into classes?
My last attempt was to just try to get something simple out of this and am getting a NullPointerException on the second of these lines:
userDet = new JSONObject(string);
JSONArray userDetJson = userDet.getJSONArray("Steps");
change it to "steps" and not "Steps" , It will fix it:
userDet = new JSONObject(string);
JSONArray userDetJson = userDet.getJSONArray("steps");
The full parsing method:
JSONArray mainArray = new JSONArray(json);
for (int i = 0 ; i < mainArray.length() ; i ++)
{
JSONObject currentItem = array.getJSONObject(i);
String title = currentItem.getString("title");
String body = currentItem.getString("body ");
....
JSONArray currentItemStepsArray = currentItem.getJSONArray("steps");
for (int j = 0 ; j < currentItemStepsArray.length() ; j ++)
{
....
}
}
Here, try this:
JSONArray topLevelArr = new JSONArray(json);
JSONArray stepArr = topLevelArr.getJSONObject(0).getJSONArray("steps");

Categories