I am new to the android development. i am having the following android code to call the json
try {
JSONObject jsonObject = new JSONObject(result);
//JSONObject object = jsonObject.getJSONObject("CUSTOMER_ID");
JSONArray jArray = new JSONArray(CUSTOMER_ID);
returnUsername1 = jArray.getInt("CUSTOMER_ID");
Toast.makeText(getApplicationContext(), ""+returnUsername1,Toast.LENGTH_LONG).show();
for (int i = 0; i < jArray.length(); i++) {
}
My JSON format is like [[{"0":"1","CUSTOMER_ID":"1"}]].
i refer some json format it should like [{"0":"1","sno":"1"}] i can understand this.But mine is different from this.
how can i call the customer_id using the above code.anyone can suggest a solution.
What you have is a Json Array
JSONArray jsonarray = new JSONArray(result); // result is a Array
[ represents json array node
{ represents json object node
Your Json. Do you need a Json array twice?
[ // array
[ //array
{ // object
"0": "1",
"CUSTOMER_ID": "1"
}
]
]
Parsing
JSONArray jsonArray = new JSONArray(result);
JSONArray ja= (JSONArray) jsonArray.get(0);
JSONObject jb = (JSONObject) ja.get(0);
String firstvalue = jb.getString("0");
String secondvalue = jb.getString("CUSTOMER_ID");
Log.i("first value is",firstvalue);
Log.i("second value is",secondvalue);
LogCat
07-22 14:37:02.990: I/first value is(6041): 1
07-22 14:37:03.048: I/second value is(6041): 1
Generally, to get a JSONObject from a JSONArray:
JSONObject jsonObject = jsonArray.getJSONObject(0); //0 -> first object
And then
int userID = jsonObject.getInt("CUSTOMER_ID");
CUSTOMER_ID is not considered a JSONObject in this case. If jsonObject is what you think it is, then you should be able to access it with jsonObject.getString("CUSTOMER_ID")
if you have any problems regarding to your JSON format first validate it through this site
http://jsonlint.com/
then for parsing
JSONObject jsonObject = new JSONObject(result);
// since your value for CUSTOMER_ID in your json text is string then you should get it as
// string and then convert to an int
returnUsername1 = Integer.parseInt(jsonObject.getString("CUSTOMER_ID"));
Try this
JSONObject jsonObject = new JSONObject(result);
JSONArray jArray =json.getJSONArray("enterjsonarraynamehere");
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject oneObject = jArray.getJSONObject(i);
// Pulling items from the array
String cust= oneObject.getString("CUSTOMER_ID");
} catch (JSONException e) {
// Oops something went wrong
}
}
Im assuming your json is something like this
<somecode>
{
"enterjsonarraynamehere": [
{ "0":"1",
"CUSTOMER_ID":"1"
},
{ "0":"2",
"CUSTOMER_ID":"2"
}
],
"count": 2
}
<somecode>
Related
String json =
[
{"a":{"aa":"string","ab":"string"} },
{"b":{"ba":"string","bb":"string"} }
]
I am trying to parse this data using JsonObject.
When i use this code :
JSONStringer Js= new JSONStringer(json);
Log.d("json", ""+Js);
it gives me this : (the first line of json but i want all data)
{"a":{"aa":"string","ab":"string"} }
How can I read this with Android ?
It is a JsonArray not a simple JsonObject
try this:
JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) {
JSONObject row = a.getJSONObject(i);
Log.d("json", ""+row);
}
Actually, this JSON is kind of weird, as it wraps the array objects into one additional object each ("a" and "b"), which will make it hard to parse. It should look like this:
String json = [ {"name":"a","aa":"string","ab":"string"},
{"name":"b","ba":"string","bb":"string"} ]
And then you would parse it like this:
JSONArray root = new JSONArray(jsonString); //this is the above string
for(int i = 0; i < root.length(); i++){
JSONObject current = root.getJSONObject(i);
//Do the parsing here
}
parsing does not work json Android
parsing does not work json Android
{
"group_name":"МБА-14",
"days":[
{
"weekday":1,
"lessons":[
{
"subject":"Научно-исследовательский семинар",
"type":0,
"time_start":"17:10",
"time_end":"18:30",
"time_number":6,
"parity":1
}
]
}
]
}
I need to get the value weekday. This is my code:
JSONObject jsonObject=new JSONObject(str);
JSONObject jsonObject1=jsonObject.getJSONObject("weekday");
Log.e("aaaa", jsonObject1.toString() );
JSONArray days = jsonObject.getJSONArray("days");
JSONObject oneDay = days.getJSONObject(0);
int weekday = oneDay.getInt("weekday");
If your "str" variable contains this,
then parse like
JSONObject jsonObject=new JSONObject(str);
JSONArray jArray = jsonObject.getJSONArray("days");
String weekday = (jArray .getJSONObject(0)).getString("weekday");
I have some problem... when i try to populating spinner data from json
this, my json data :
parse bank and product work normally
Bank.
Product.
Provider no data
Log.
what should i do to parse provider to android spinner ?
Try this..
provider is inside product array
"product": [ // JSONArray
{ // JSONObject
"id": "1",
"value": "PULSA",
"name": "Pulsa Telpon",
"provider": [ // JSONArray
Code
try {
JSONObject jsonObj = new JSONObject(json);
if (jsonObj != null) {
JSONObject product_obj = jsonObj.getJSONArray("product").getJSONObject(0);
JSONArray categories = product_obj
.getJSONArray("provider");
for (int i = 0; i < categories.length(); i++) {
JSONObject catObj = (JSONObject) categories.get(i);
Category cat = new Category(catObj.getInt("id"),
catObj.getString("name"));
categoriesList.add(cat);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Here you are trying to fetch provider jsonArray from the jsonobject formed using the json response json.
JSONArray categories = jsonObj.getJSONArray("provider");
however the provider jsonArray is not in the base jsonobject, but it is in the product jsonArray. That is why you are not getting any data for provider.
Solution :
first get the product jsonarray from the base jsonobject. Then extract the provider jsonarray for each product in loop.
Algorithm or Flow:
JSONObject jsonObj = new JSONObject(json); // Base json object
JSONArray bankArray = jsonObj.getJSONArray("bank"); // Bank json array
JSONArray productArray = jsonObj.getJSONArray("product"); // Product json array
// no. of items in product
int arrayLength = productArray.length();
for (int i = 0; i < arrayLength; i++) {
// get single product object from array
JSONObject singleProductObject = productArray
.getJSONObject(i);
// Get the provider array for every product
JSONArray providerArray = singleProductObject.getJSONArray("provider");
}
This way you will get the provider data.
Hope it helps you.
What I understand your Json object contains to memeber that is a array bank and product
provider is inner array od product.
I am not sure if you take care of this
try this only for single product
JSONArray categories = jsonObj.getJSONArray("product")[0].getJSONArray("provider");
I'm trying to take the following string that I got as a json object:
[
{
"id": "picture1",
"caption": "sample caption",
"picname": "sample picture name"
}
]
and turn it into a array so I can populate a list
I've tried turning it into a jsonarray by doing this:
JSONArray myjsonarray = myjson.toJSONArray(string_containing_json_above);
but that didn't seem to work.
==============
Here is full code with the working solution
myjson = new JSONObject(temp);
String String_that_should_be_array = myjson.getString("piclist");
JSONArray myjsonarray = new JSONArray(String_that_should_be_array);
For(int i = 0; i < myjsonarray.length(); i++){
JSONObject tempJSONobj = myjsonarray.getJSONObject(i);
showToast(tempJSONobj.get("caption").toString());
}
temp is the json from the server
Issue is here:
JSONArray myjsonarray = myjson.toJSONArray(temparray);
Solution:
JSONArray myjsonarray = new JSONArray(myJSON);
// myJSON is String
Now here you are having JSONArray, iterate over it and prepare ArrayList of whatever types of you want.
here you get JSONArray so change
JSONArray myjsonarray = myjson.toJSONArray(temparray);
line as shown below
JSONArray jsonArray = new JSONArray(readlocationFeed);
and after
JSONArray jsonArray = new JSONArray(readlocationFeed);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
explrObject.getString("caption");
}
JSONArray isn't working because the JSON you provided is not an array. You can read more about JSON syntax here: http://www.w3schools.com/json/json_syntax.asp
In the meantime, you could manually create your array by paring the JSON one string at a time.
JSONObject strings = new JSONObject(jsonString);
String array[] = new String[5];
if (jsonString.has("id"){
array[0] = jsonString.getString("id");
}
if (jsonString.has("caption"){
array[1] = jsonString.getString("caption");
}
...
etc.
i want to extract the given json arry in android,
[{"Outofserviceday":{"outofservice":"2013-02-22"}},
{"Outofserviceday":{"outofservice":"2013-02-27"}},
{"Outofserviceday":{"outofservice":"2013-02-28"}}]
i have the code for extracting the json data like given below
[{"Requestcard":{"id":"994","userprofile_id":"14","userprofile_name":"Syed
Imran","company_name":"DLF Akruti Park, Hinjewadi, Pune,
Maharashtra","sex":"male","travel_date":"2013-02-12"}}]
in this case we can retrive the json boject using the code
JSONObject menuObject = json_data.getJSONObject("Requestcard");
and retrieve each element by
requestid= menuObject.getString("id");
But in the first case how we identify each Outofserviceday in the json array ? and How extract each data ???
you can do something like below, can create jsonArray from string json data and then can extracts json objects in a loop or so.
String json =" [{\"Outofserviceday\":{\"outofservice\":\"2013-02-22\"}}]"; //json-data which is basically a json array
JSONArray jArray = new JSONArray(json); / creating an jsonarray
for (int i = 0; i < jArray.length(); i++) {
// you can have jsonObject from json array here in the loop
}
Try this:
JSONObject json = new JSONObject(result);
JSONArray json1= json.getJSONArray("data");
if (json1.length()!=0) {
for (int i = 0; i < json1.length(); i++) {
String name = json1.getJSONObject(i).getString("name");
}
}
With the help of below code you can retrieve the value of outofservice
JSONArray jArray = new JSONArray(your data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jOutOfServiceDay = jArray.getJSONObject(i);
JSONObject jobj = jOutOfServiceDay.getJSONObject("Outofserviceday");
Log.i("Required data is:", "" + jobj.getString("outofservice"));
}