I'm trying to parse the json format:
{"data":["Laptop","Desktop","Ultrabook"]}
but this seems to give me an error:
JSONArray jr = new JSONArray(jsonStr);
I tried to parse the strings instead, but i doubt thats the best practice.
And, I couldn't figure out how to parse the quotations out of the string.
Change this
JSONArray jr = new JSONArray(jsonStr);
to
JSONObject jsonobject = new JSONObject(jsonStr);
Then
JSONArray jr = jsonobject.getJSOnArray("data");
for(int i=0;i<jr.length();i++)
{
String value =(String) jr.get(i);
}
Your json
{ // json object node
"data": [ // json array data
"Laptop",
"Desktop",
"Ultrabook"
]
}
That's because you are not getting a JSONArray but JSONObject (the root element is not an array). Try this:
JSONObject data = new JSONObject(jsonStr).getJSONArray("data");
do :
JSONObject obj = new JSONObject(jsonStr);
JSONArray arr = obj.getJSONArray("data");
your first tag in JSONObject not a JSONArray.
So you need to change this from
JSONArray jr = new JSONArray(jsonStr);
to
JSONObject jObject = new JSONObject(jsonStr);
try like this
String data = "{\"data\":[\"Laptop\",\"Desktop\",\"Ultrabook\"]}";
try {
JSONObject jObj = new JSONObject(data);
JSONArray jsonArray = new JSONArray();
jsonArray= jObj.optJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println(jsonArray.get(i).toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
I have know to parser JSON array in single array but how to pass multiple JSON array and set it value to require
{"scode":"200","all_menu":[{"app_menu_id":"67","app_menu_name":"Demograpics","all_sub_menu":[{"app_menu_id":"67","app_sub_menu_id":"47","app_sub_menu_name":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/8451504072003.jpg"}],"sub_menu":"true"},{"app_menu_id":"68","app_menu_name":"Lyrics","all_sub_menu":[{"app_menu_id":"68","app_sub_menu_id":"48","app_sub_menu_name":"Music","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4681504072092.jpg"}],"sub_menu":"true"},{"app_menu_id":"69","app_menu_name":"Adult","all_sub_menu":[{"app_menu_id":"69","app_sub_menu_id":"49","app_sub_menu_name":"Double
Meaning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/9931504072151.jpg"}],"sub_menu":"true"},{"app_menu_id":"70","app_menu_name":"Emotions","all_sub_menu":[{"app_menu_id":"70","app_sub_menu_id":"50","app_sub_menu_name":"Love","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/7611504072164.jpg"}],"sub_menu":"true"},{"app_menu_id":"71","app_menu_name":"Wishes","all_sub_menu":[{"app_menu_id":"71","app_sub_menu_id":"51","app_sub_menu_name":"Good
Morning","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/5171504072183.jpg"}],"sub_menu":"true"},{"app_menu_id":"72","app_menu_name":"Among Friend","all_sub_menu":[{"app_menu_id":"72","app_sub_menu_id":"52","app_sub_menu_name":"Friendship","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4411504072205.jpg"}],"sub_menu":"true"},{"app_menu_id":"73","app_menu_name":"Jokes","all_sub_menu":[{"app_menu_id":"73","app_sub_menu_id":"53","app_sub_menu_name":"Santa
Banta","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4331504072225.jpg"}],"sub_menu":"true"},{"app_menu_id":"74","app_menu_name":"Featured","all_sub_menu":[{"app_menu_id":"74","app_sub_menu_id":"54","app_sub_menu_name":"Ganpati
Bappa","app_sub_menu_image":"http:\/\/app.hindipublic.com\/app_allwhatsupstatus\/menu\/medium\/4771504072247.jpg"}],"sub_menu":"true"}]}
Suppose "response" is your JSONResponse
JSONObject jsonObject = new JSONObject(response);// This is used to get jsonObject from response
String sCode=jsonObject.optString("scode"); // This is how you can parse string from jsonObject
JSONArray allmenuArray=jsonObject.optJSONArray("all_menu"); //This is how you can parse JsonArray from jsonObject
for(int i=0;i<allmenuArray.length();i++){
JSONObject objectJson=allmenuArray.optJSONObject(i);//This is how you can parse jsonObject from jsonArray
}
Like This you can parse all your jsonObject and jsonarray. Just follow these steps you can easily parse your full JSONResponse
Try this.
try {
JSONObject jsonObject = new JSONObject("JSONResponse");
String scode = jsonObject.optString("scode");
JSONArray allmenuArray = jsonObject.optJSONArray("all_menu");
for (int i = 0; i < allmenuArray.length(); i++) {
JSONObject objectJson = allmenuArray.optJSONObject(i);
boolean sub_menu = objectJson.getBoolean("sub_menu");
String app_menu_id = objectJson.getString("app_menu_id");
String app_menu_name = objectJson.getString("app_menu_name");
JSONArray all_sub_menu = objectJson.getJSONArray("all_sub_menu");
for (int j = 0; j < all_sub_menu.length(); j++) {
JSONObject data = allmenuArray.optJSONObject(j);
Log.e("app_menu_id", data.getString("app_menu_id"));
Log.e("app_sub_menu_id", data.getString("app_sub_menu_id"));
Log.e("app_sub_menu_name", data.getString("app_sub_menu_name"));
Log.e("app_sub_menu_image", data.getString("app_sub_menu_image"));
}
}
} catch (JSONException e) {
Log.e("ERROr", e.toString());
}
I am working in Android and finding json from internet that looks like this:
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
I want to find C21 that is in the A. See the json coming from request.
Can someone help me?
try {
JSONObject j=new JSONObject(data);
JSONArray c= null;
c = j.getJSONArray("This");
JSONObject item=c.getJSONObject(0);
JSONArray me=item.getJSONArray("me");
for(int pos=0;pos<me.length();pos++)
{
JSONObject childObject=me.getJSONObject(pos);
String fisrtkey=childObject.getString("A");
JSONArray jsonArray=childObject.getJSONArray("c");
}
} catch (JSONException e1) {
e1.printStackTrace();
}
//hope this will help you and also check json is invalid or not ,you missed
// bracket of jsonarray "me".
You have missed THIS json array while parsing. First get that and from that json object and then get ME json array. Something like this
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("This");
JSONObject j1 = c.getJSONArray(0);
JSONArray d = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject item = c.getJSONObject(n);
System.out.println(item.getString("A"));
}
Try this:
JSONObject j = new JSONObject(data);
JSONArray c = j.getJSONArray("me");
for(int n = 0; n < c.length(); n++) {
JSONObject person = (JSONObject) c.get(n );
String id = person.getString("A");
...
}
My application connects to the web and retrieves a JSON file. I am having some issues retrieving the data that I need from this file. Here is the link to the JSON file: http://api.wunderground.com/api/f9d9bc3cc3834375/forecast/q/CA/San_Francisco.json
Here is a snippet of what it looks like:
I would like to get the value of the "period" variable in the first JSon object within the "forecastday" array, which should be 0. Here is how i'm looking at this. "forecastday" is the array, within that there are a number of JSon objects, each containing variables like "period","icon"....."pop".
In my code, I attempt to get the JSON array "forecastday" and then get the first Json object of the array, I then retrieve the value of "period" in that object and set it in a TextView:
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray contacts = jsonObj.getJSONArray("forecastday");
JSONObject c = contacts.getJSONObject(0);
String period = c.getString("period");
responseView.setText(period);
} catch (JSONException e) {
e.printStackTrace();
}
}
When I run the code, nothing is being retrieved. I'm new to working with JSon and am wondering if I'm looking at this wrong. Please help.
Your "forecastday" JSONArray is inside the "txt_forecast" JSONObject that is inside the "forecast" JSONObject of your response, so you have to extract your JSONArray from this JSONObject and not from the root JSON response :
try {
JSONObject jsonObj = new JSONObject(response);
--> JSONObject forecatsObj = jsonObj.getJSONObject("forecast");
--> JSONObject txtForecatsObj = forecatsObj.getJSONObject("txt_forecast");
JSONArray contacts = txtForecatsObj.getJSONArray("forecastday");
...
You were close but it is incorrect, try like this:
JSONObject jsonObj = new JSONObject(response);
JSONObject forecast = jsonObj.getJSONObject("forecast");
JSONObject txtForecast = forecast.getJSONObject("txt_forecast");
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
//parse the first period value
String period = forecastDay.getJSONObject(0).getString("period");
responseView.setText(period);
Try this code :
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject forecastObj = jsonObj.getJSONObject("forecast");
JSONObject txt_forecastObj = forecastObj.getJSONObject("txt_forecast");
JSONArray foracastdayArray = txt_forecastObj.getJSONArray("foracastday");
JSONObject oOjb0 = foracastdayArray.getJSONObject(0);
String period = oOjb0.getString("perioed");
}catch (Exception e){
}
This code works for me fine:
JSONObject jsonResponse = new JSONObject(responce);
JSONArray jsonMainNode = jsonResponse.optJSONArray("forecastday");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String period = jsonChildNode.optString("period");
responceView.setText(period);
}
I want to return this back to my android emulator. This is a combination of jsonArray and jsonObject
[{"id":"11WAD01442","name":"Teng Kwang Wei"},{"id":"11WAD01443","name":"test 1"},{"id":"11WAD01444","name":"test 2"},{"id":"11WAD01445","name":"test 3"},{"status":true}]
What should I use to receive this json encode.
JSONArray jsonArray = new JSONArray(content);
or
JSONObject jsonObject = new JSONObject(content);
You need to decode the top object. In your case is an Array (of JSONObjects):
JSONArray jsonArray = new JSONArray(content);
for (int i=0, len=jsonArray.length(); i<len; i++) try {
JSONObject obj=jsonArray.getJSONObject(i);
String id=obj.getString("id");
String name=obj.getString("name");
.
.
Log.d("DUMP", "User "+i+": id="+id+", name="+name);
} catch (JSONException e) {
Log.d("DUMP", "Problem with user");
}
l get string with answer from server. l want to do it JSONObject.
l do
JSONObject jsonObj = new JSONObject(json);
in json has
"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
but jsonObj has
"{"sentences":[{"src_translit":"RẑRg̀RoRẐ","orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}],"server_time":1,"src":"ru"}"
so how l can get value from "trans":"RѕRґReRЅ"?
PS RѕRґReRЅ it's "own" in normal charset
If you already have the jsonObj you just have to do:
try {
// Getting Array of Sentences
sentences = json.getJSONArray("sentences");
// looping through All Contacts
for(int i = 0; i < sentences.length(); i++){
JSONObject c = sentences.getJSONObject(i);
// get the value from trans
String trans = c.getString("trans");
//now you should save this string in an array
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
JSONObject jsonObj = new JSONObject(json);
JSONArray sentences = jsonObj.getJSONArray("sentences");
for(int i=0;i<sentences.length();i++){
JSONObject number = sentences.getJSONObject(i);
String transValue = number.getString("trans");
}
First you have to remove single quote form starting and from ending of your JSON string (json), by this your JSON will be a valid json for this you have to do like :
json= json.substring(1, json.length()-1);
After that you do like this :
JSONObject oJsonObject = new JSONObject(json);
JSONArray oJsonArray = oJsonObject.getJSONArray("sentences");
for(int i=0; i<oJsonArray.length(); i++)
{
JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
String transValue = oJsonObject1 .getString("trans");
}