Convert JSON string in array format into an array in java - android

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.

Related

How to read this JSON

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
}

JSONObject or JSONArray

I have data on this string, but I'm having trouble to access the individual data.
This is my code:
Log.d("Detail Outputss", "" + response.toString());
And this is the string output:
{"futsal_id":"45","info":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"description":[{"futsal_id":"45","futsal_desc":"Futsal is the fever which never ends.\r\nKathmandu futsal is - a platform, a medium of communication for our fraternity. Not just somewhere to host challenges or seek venues, but a place where we can all share and spread the luv! Here, you can find just about anything, do just about anything, and see just about anything. Unbelievable? Believe it."}],"features":[{"futsal_id":"45","futsal_feat":"Free Wifi"},{"futsal_id":"45","futsal_feat":"High Quality Grass"},{"futsal_id":"45","futsal_feat":"Canteen Facility"},{"futsal_id":"45","futsal_feat":"Friendly Environment"}],"dimension":[{"futsal_id":"45","dimension":"40m X 20m"}],"no_of_futsal":[{"futsal_id":"45","number":"1"}],"opening_hrs":[{"futsal_id":"45","open_time_id":"1","open_time":"6am","close_time_id":"15","close_time":"9pm"}],"price_weekdays_price1":[{"futsal_id":"45","price_id":"1","start_time":"6am","end_time":"12pm","price":"1200"}],"price_weekdays_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1000"}],"price_weekdays_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"1500"}],"price_weekend_price1":[{"futsal_id":"45","price_id":"1","start_time":"6pm","end_time":"12pm","price":"1500"}],"price_weekend_price2":[{"futsal_id":"45","price_id":"2","start_time":"12pm","end_time":"6pm","price":"1800"}],"price_weekend_price3":[{"futsal_id":"45","price_id":"3","start_time":"6pm","end_time":"9pm","price":"2000"}],"phone_number_address":[{"futsal_id":"45","futsal_name":"Kathmandu Futsal","city":"Kathmandu","address":"Kathmandu","owner_name":"Hari Prasad","owner_address":"Kathmandu","email":"kathmandufutsal#gmail.com","password":"kathmandu","phone_no":"1111111","mobile_no":"9841112233","status":"1"}],"news":[{"futsal_id":"45","news_id":"7","news_title":"asdfasdfasdfasdf","news_description":"asdfashdflahsdlfasdjf;oajsd;ofjaosdijfoaisdjofajsdfja;sjdf;ajsd;fja;ksdjf;kasjd;fkja;sdjf;asjd;fajsd;fjasdjf;asjd;fjas;djf;asdf"},{"futsal_id":"45","news_id":"6","news_title":"awefasdf","news_description":"sdjf;asdf;a;sdf;asjdf;oajsd;fja;sdjf;oajsd;oifjaosdf"},{"futsal_id":"45","news_id":"5","news_title":"asdfasdf","news_description":"sadfasdfasdfasdfa"}]}
I want to get the value of each object futsal_id, futsal_name, city and others, thanks in advance!!
It is JSONArray within a JSONObject
JSONObject json = new JSONObject(response);
JSONArray info = json.getJSONArray("info");
//getting the first value.. loop it if you have more than one
JSONObject infoObject = info.getJSONObject(0);
JSONArray description = json.getJSONArray("description");
JSONObject json = new JSONObject(response);
int fustal_id =Integer.parseInt(json.getString("futsal_id"));
JSONArray inf = json.getJSONArray("info");
JSONObject info = inf.getJSONObject(0);
...
JSONObject responseObject = new JSONObject(response);
JSONArray info = responseObject.getJSONArray("info");
for(int i = 0; i < info.length(); i++) {
JSONObject obj = info.getJSONObject(i);
String futsal_id = obj.getString("futsal_id");
String futsal_name = obj.getString("futsal_name");
//so on
}
Be sure to catch the exceptions.

How to split this kind of data in android

Here is the
string one =[{"ID":5,"Name":"Sai"}]
how i get only id and name from this string
Matcher matcher = Pattern.compile("\\[([^\\]]+)").matcher(one);
List<String> tags = new ArrayList<String>();
int pos = -1;
while (matcher.find(pos+1)){
pos = matcher.start();
tags.add(matcher.group(1));
}
System.out.println("getting data"+tags);
i tried this but it didn't work
List<String> ls = new ArrayList<String>(one);
JSONArray array = new JSONArray();
for(int i = 0; i< array.length(); i++){
JSONObject obj = array.getJSONObject(i);
ls.add(obj.getString("Name"));
}
It's JSON format and it can very easily be read in Android. Here is the sample code:
JSONArray array = new JSONArray(one);
int length = array.length();
for(int i=0;i< length; i++)
{
JSONObject temp = array.getJSONObject(i);
System.out.println(temp.getString("ID"));
System.out.println(temp.getString("Name"));
}
This format of data is called JSON.
Have a look at Go to http://json.org/, scroll to (almost) the end, click on one of the many Java libraries listed.
First of all, your string initialization is wrong.
Wrong:
string one =[{"ID":5,"Name":"Sai"}]
Correct:
String one ="[{\"ID\":5,\"Name\":\"Sai\"}]";
Second, its a JSON formatted data so you can parse it using JSONArray and JSONObject classes, instead of creating any pattern.
Now, in your case its JSONObject inside JSONArray so initially create an object of JSONArray using your string.
For example:
JSONArray arrayJSON = new JSONArray(one); // 'one' is your JSON String
for(int i=0; i<arrayJSON.length(); i++) {
JSONObject objJson = arrayJSON.getJSONObject(i);
String ID = objJson.getString("ID");
.....
.....
// same way you can fetch/parse any string/value from JSONObject/JSONArray
}
it is a json formate Date
use JsonObject class to parse this data
tutorial this
JSONArray jsonArray = new JSONArray("[{\"ID\":5,\"Name\":\"Sai\"}]");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("ID"));
System.out.println(object.getString("Name"));
}

How to call the JSON object in Android

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>

Json data extracting in android

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"));
}

Categories

Resources