I want to fetch string values from a JSON-array and set it to a spinner in Android. How can I
do this?
`{"languages":["English","Hindi","Punjabi","",""]} `languages":["English","Hindi","Punjabi","",""]}
You can parse your JSONARRAY like this
if (jsonResultArry != null) {
for (int i = 0; i < jsonResultArry.length(); i++) {
try {
JsonObject jsonPblm = jsonResultArry.getJSONObject(i);
pblmId = jsonPblm.getString("id");
ticketDate = jsonPblm.getString("ticket_date");
pblmName = jsonPblm.getString("description");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related
I wanna delete the value inside this jsonArray:
{"category_ids":[0,1,2,3,4,5],"keyword":""}
For example, i wanna remove 0 from category_ids jsonArray.
Can anybody help me? Thanks in advance.
String load = "{\"category_ids\":[0,1,2,3,4,5],\"keyword\":\"\"}";
try {
JSONObject jsonObject_load = new JSONObject(load);
JSONArray jsonArray = jsonObject_load.getJSONArray("category_ids");
Log.d("out", RemoveJSONArray(jsonArray,1).toString());
} catch (JSONException e) {
e.printStackTrace();
}
public static JSONArray RemoveJSONArray( JSONArray jarray,int pos) {
JSONArray Njarray = new JSONArray();
try {
for (int i = 0; i < jarray.length(); i++) {
if (i != pos)
Njarray.put(jarray.get(i));
}
} catch (Exception e) {
e.printStackTrace();
}
return Njarray;
}
Below API 19
String result = "{\"category_ids\":[0,1,2,3,4,5],\"keyword\":\"\"}";
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("category_ids");
jsonArray = removeValue(jsonArray, 0); // below api 19
// jsonArray.remove(0); // above api 19
} catch (JSONException e) {
e.printStackTrace();
}
public JSONArray removeValue(JSONArray jsonArray, int index) {
JSONArray copyArray = new JSONArray();
try {
if (index < jsonArray.length()) {
for (int i = 0; i < jsonArray.length(); i++) {
if (i != index) {
copyArray.put(jsonArray.get(i));
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return copyArray;
}
Above API 19 use jsonArray.remove(0);
I hope this will help you.
I have a JSON data which is accessible through a link like http://192.55.23.210:8085/Services/getFriends?userId=xyz which returns an array like ["xyz","abc"]. How to access these objects
You can get the field names without having to know them using names(int) method of JSONObject class, and then access them:
public void getFieldNames(JSONArray jsonArray) {
List<String> fieldNames = new ArrayList<String>;
try {
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
fieldNames[i] = jsonObject.names(i);
}
} catch (Exception e) {
Log.e("ConnectToDatabase->getJsonData", "Error Parsing JSON Data "
+ e.toString());
}
}
JSONParser parser=new JSONParser();
String s="[\"xyz\",\"abc\"]";
try {
JSONArray jsonarray=(JSONArray)parser.parse(s);
for (Object object : jsonarray) {
System.out.println((String)object);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a api call that returns this json object:
{
"elenco": [
"folder 1",
"folder 2",
"folder 3"
],
"codice": "123456789"
}
and this is my piece of code that get the result:
protected void onPostExecute(JSONObject result) {
// Close progress dialog
Dialog.dismiss();
JSONObject jobj = null;
String codice_utente = null;
JSONArray elenco_cartelle = null;
try {
jobj = new JSONObject(String.valueOf(result));
} catch (JSONException e) {
e.printStackTrace();
}
//Provo a recuperare i campi json
try {
codice_utente = jobj.getString("codice");
elenco_cartelle = jobj.getJSONArray("elenco");
} catch (JSONException e) {
e.printStackTrace();
}
//This should fetch the elenco array
for (int i = 0; i < elenco_cartelle.length(); i++) {
JSONObject childJSONObject = null;
try {
childJSONObject = elenco_cartelle.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
//Can't use getString here...
}
}
How can I fetch the elenco part? I do not have a key to use, so how can I add every row in an ArrayList?
I tried to use a for but I have no idea about how to get the rows
Use JSONArray.getString() http://developer.android.com/reference/org/json/JSONArray.html#getString(int)
for (int i = 0; i < elenco_cartelle.length(); i++) {
String content = null;
try {
content = elenco_cartelle.getString(i);
} catch (JSONException e) {
e.printStackTrace();
}
//Can't use getString here...
}
You now have your content on content
Currently I'm using this code to receive the list of friends.
JSONArray array = user.getJSONArray("friendsArray");
for(int i = 0; i < array.length(); i++){
try {
String obj = array.getString(i);
adapter.add(new FriendsList(obj), 0);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How can I grab "statuses" from a different class but order posts from the users friends by date.
I use library JsonHttpResponseHandler
and this my code
Data JSON is =
[{"id":"4","2":"123","phone":"123","1":"Shin","0":"4","name":"Shin"},{"id":"5","2":"555","phone":"555","1":"Wolf","0":"5","name":"Wolf"},{"id":"6","2":"666","phone":"666","1":"Lunar","0":"6","name":"Lunar"}]
And this my code =
#Override
public void onSuccess(int statusCode, org.apache.http.Header[] headers, org.json.JSONArray response)
Question is how can i use response data in for loop
Use below code ,
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jobj = response.getJSONObject(i);
String id = jobj.getString("id");
String two = jobj.getInt("2");
String phone = jobj.getInt("phone");
String one = jobj.getInt("1");
String zero = jobj.getInt("0");
String name = jobj.getString("name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
it is the same as array.
for(int i =0 ; i < response.length() ; i++){
JSONObject object = response.getJSONObject(i);
}
inside json array > jsonobject it is like using a List
for your reference : JSON Array iteration in Android/Java
String dataStr="[{\"id\":\"4\",\"2\":\"123\",\"phone\":\"123\",\"1\":\"Shin\",\"0\":\"4\",\"name\":\"Shin\"},{\"id\":\"5\",\"2\":\"555\",\"phone\":\"555\",\"1\":\"Wolf\",\"0\":\"5\",\"name\":\"Wolf\"},{\"id\":\"6\",\"2\":\"666\",\"phone\":\"666\",\"1\":\"Lunar\",\"0\":\"6\",\"name\":\"Lunar\"}]";
try {
JSONArray jsonStrs =new JSONArray("1111");
for(int i=0;i<jsonStrs.length();i++)
{
JSONObject jobj=jsonStrs.getJSONObject(i);
int id=jobj.getInt("id");
String phone=jobj.getString("phone");
//get other values
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}