here is my code..in android
String
result[{"Submitted_Date_Time":"12\/3\/2012","City":"","Issue_Category":"Graffity","OnProgress_Date_Time":"","id":"000000000000000","area":"SH 55","State":" ","Issue_Description":" ","Closed_Date_Time":"","imagepath":"android.graphics.Bitmap#40d55cd8","Latitude":"23.71","Longitude":"72.04","Issue_Status":"Closed","LandMark":" "}]
and i try to convert it in to json array and when i want to retrive json object from json array it gives me nullpointer exception...
JSONArray jarray = new JSONArray(result);
JSONObject jobj = jarray.getJSONObject(1);
plz help me.. thnkx in advance..
first your json String if it's contain "result" then it is not valid you can check it here http://jsonviewer.stack.hu/
to make valid json string just use String.replace as:
String finaljson=result.replace("result", "");
now JSON String is valid you can parse it as:
JSONArray jsonarray = new JSONArray(finaljson);
for (int i = 0 ; i < jarray.length() ; i++) {
JSONObject jsonobj = jarray.getJSONObject(i);
// get value from json object here
String str_City=jsonobj.getString("City");
///....
}
The Json array contains only one entry, the index is 0
JSONArray jarray = new JSONArray(result);
JSONObject jobj = jarray.getJSONObject(0);
To handle it right, access the json array using a for loop
JSONArray jarray = new JSONArray(result);
for (int i = 0 ; i < jarray.length() ; i++) {
JSONObject jobj = jarray.getJSONObject(i);
.....
}
Update your below code line it will solve your problem.
JSONObject jobj = jarray.getJSONObject(0);
or you can write below code instead of your above code line if object is more than one.
for(int i=0;i<jarray.length();i++){
JSONObject jobj = jarray.getJSONObject(i);
}
Related
I have a StringBuilder:
My code is the following:
String response = sb.toString();
// logger.info("response is '{}' ", response);
JsonObject jsonObject = new JsonObject(response);
JsonObject fileStatuses = jsonObject.getJsonObject("FileStatuses");
JsonArray fileStatus = (JsonArray) fileStatuses.getJsonArray("FileStatus");
for (int i = 0; i < fileStatus.size(); ++i) {
JsonObject rec = fileStatus.getJsonObject(i);
String pathSuffix = rec.getString("pathSuffix");
logger.info(" the pathSuffix value is '{}' ", pathSuffix );
}
I want to use the javax.json.JsonObject and not org.json.JsonObject.
So, the problem is in new JsonObject(response); ==> Cannot instantiate the type JsonObject.
I know that org.json.JSONObject has a constructor that takes a String, but I think not the case when using javax.json.JsonObject.
How can I correct my code to make JsonObject jsonObject = new JsonObject(response) take a String and stay using javax.json.JsonObject class?
According to its API instead of using JsonObject jsonObject = new JsonObject(response); you can instantiate it from your response-String like this:
String response = sb.toString();
StringReader reader = new StringReader(response);
JsonReader jsonReader = Json.createReader(reader);
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();
im kinda new using JSON, and everything was fine until i have to make this JSON format.
"function":"ListarHoteles",
"parameters":[""]
Right now my code:
JSONObject JSONarr = new JSONObject();
JSONArray pam = new JSONArray();
jo.put("function", "ListarHoteles");
pam.add(" ");
JSONObject mainOBJ = new JSONObject();
mainOBJ.put("parameters", pam);
And im receiving:
{"{\"function\":\"ListarHoteles\"}":{},"parameters":[" "]}
Thank you
Is this what you want?
JSONObject jsonObject = new JSONObject();
jsonObject.put("function", "ListarHoteles");
JSONArray arr = new JSONArray();
arr.add("");
jsonObject.put("parameters", arr);
System.out.println(jsonObject.toString(2));
Output:
{
"function":"ListarHoteles",
"parameters":[
""
]
}
I am facing a problem to reach to an object which is inside an array of another object of another array. To make it simpler i am attaching the image (attached below), basically i want to reach to json object "0" inside jsonarray ticket itmes.
my codes are
JSONArray jsonArray = new JSONArray(buffer.toString());
JSONObject jsonobject = null;
jsonobject = jsonArray.getJSONObject(0);
JSONArray jsonarray1 = new JSONArray(buffer.toString());
jsonarray1 = jsonobject.getJSONArray("ticketitems");
JSONObject jsonobject1 = null;
TicketItem ticketItemList = new TicketItem();
jsonobject1 = jsonArray.getJSONObject(0);
ticketItemList.setItemCount(jsonobject1.getInt("itemCount"));
TicketItemList.add(ticketItemList);
However, jsonarray1 generates an exception stating
org.json.JSONException: No value for ticketitems
enter image description here
replace
jsonarray1 = jsonobject.getJSONArray("ticketitems");
with
jsonarray1 = jsonobject.getJSONArray("ticketItems");
It's case sensitive. It should be
jsonarray1 = jsonobject.getJSONArray("ticketItems");
I'm a complete beginner, especially when is comes to JSON parsing.
I've been using the google directions API
to make a basic navigation app that draws a route from two locations, which is all great. However after trying for hours to parse the "steps" from the API, I still can't make it work.
I've used this example https://github.com/hiepxuan2008/GoogleMapDirectionSimple
to parse the JSON to draw the route.
This is done in the following code. (full code on github "Directionfinder.java")
private void parseJSon(String data) throws JSONException {
if (data == null)
return;
List<Route> routes = new ArrayList<Route>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
JSONObject jsonLeg = jsonLegs.getJSONObject(0);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
route.endAddress = jsonLeg.getString("end_address");
route.startAddress = jsonLeg.getString("start_address");
route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng"));
route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng"));
route.points = decodePolyLine(overview_polylineJson.getString("points"));
routes.add(route);
}
listener.onDirectionFinderSuccess(routes);
}
Heres how the JSON typically looks. http://pastebin.com/czYkBWWU
I need to parse "maneuver", "start_location", "distance", "start_location and the position from every step of a given route.
It seems fairly easy to parse this, but I just can't figure out how.
Any help will be greatly appreciated!
Here is full code. I Run it in my Device and get Successfull result. If you face problem then let me know.
try {
List<Route> routes=new ArrayList<Route>();
JSONObject mainJSON=new JSONObject(jsonJ);
JSONArray jarray1=mainJSON.getJSONArray("routes");
JSONObject jobj1=jarray1.getJSONObject(0);
JSONArray jarray2=jobj1.getJSONArray("legs");
JSONObject jobj2=jarray2.getJSONObject(0);
JSONArray stepArray=jobj2.getJSONArray("steps");
for (int i=0;i<stepArray.length();i++){
Route route=new Route();
JSONObject jobj5=stepArray.getJSONObject(i);
JSONObject disOBJ=jobj5.getJSONObject("distance");
JSONObject durOBJ=jobj5.getJSONObject("duration");
JSONObject endOBJ=jobj5.getJSONObject("end_location");
JSONObject polOBJ=jobj5.getJSONObject("polyline");
JSONObject startOBJ=jobj5.getJSONObject("start_location");
route.startAddress=jobj2.getString("start_address");
route.endAddress=jobj2.getString("end_address");
if (jobj5.has("maneuver")){
route.maneuver=new Maneuver(jobj5.getString("maneuver"));
}
route.distance=new Distance(disOBJ.getString("text"),disOBJ.getString("value"));
route.duration=new Duration(durOBJ.getString("text"),durOBJ.getString("value"));
route.startLocation=new LatLng(startOBJ.getDouble("lat"),startOBJ.getDouble("lng"));
route.endLocation=new LatLng(endOBJ.getDouble("lat"),endOBJ.getDouble("lng"));
route.points=decodePoluLine(polOBJ.getString("points"));
routes.add(route);
}
} catch (JSONException e) {
e.printStackTrace();
}
I'm new with JSON and my question might be not very hard, but I can not find the way to deal with my purpose.
I'm trying to develop the code so that I can manage some JSON content. In my case the JSON info is:
{"posts":[{"id":1a00b,"name":"Michael Thomson","info":"he is crazy"},
{"id":18,"name":"Jason Williams","info":"he is tall"}]}
Now, I'd like to get the strings from each JSON object (using Java). That's the code I have developed:
HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONArray jsonArray = jsonResult.getJSONArray("posts");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject childJSONObject = jsonArray.getJSONObject(i);
String id = childJSONObject.getString("id");
String name = childJSONObject.getString("name");
String info = childJSONObject.getString("info");
}
The error seems to be related with the sentence:
JSONArray jsonArray = jsonResult.getJSONArray("posts");
The method getJSONArray(String) is undefined for the type String
Those are the libraries I'm using to deal with
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
Thank you very much in advance!
jsonResult is a String. You need to turn it into a JSONObject first.
JSONObject obj = new JSONObject(jsonResult);
JSONArray jsonArray = obj.getJSONArray("posts");