How to iterate a Object [duplicate] - android

This question already has answers here:
Sending and Parsing JSON Objects in Android [closed]
(11 answers)
Closed 8 years ago.
As you see, I have a json. And I retrieved it . But i can retrieve the category's ID,name. But I have to change my Project to Object.
So, How can i iterate my project. Or there is any another way to do it ?

Here I just get the ID from Jsonarray,use this way to get the other jsonobject element inside the jsonarray do like that for rest of the elements.
try{
ArrayList<String>ID=new ArrayList<String>();
JSONObject jObject= new JSONObject(JsonString);
JSONArray menuObject = new JSONArray(jObject.getString("category"));
JSONArray menuObject1;
for(int i=0;i<menuObject.length;i++){
menuObject1 = new JSONArray(jObject.getString("project"));
}
for(int i=0;i<menuObject1.length;i++){
String ID= menuObject.getJSONObject(i).getString("ID");
ID.add(ID);
}
}catch(Exception e){
}

Related

How to get variable from JSON in android studio [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to get a single string from a JSON response.("oppdatert")
The following line is returned when i run my function:
{"isError":false,"errorMsg":null,"response":{"oppdatert":"ja"}}
This is an async-task, and under onTaskComplete i have the following code:
String approved = null;
JSONArray myJsonArray = JSONreturn.getJSONArray("response");
myList.clear();
for(int i = 0; i < myJsonArray.length(); i++) {
JSONObject brukerOppdatert = myJsonArray.getJSONObject(i);
approved = brukerOppdatert.getString("oppdatert");
The application crashes after "JSONArray = myJsonArray = ..."
This method have worked on other json-array, but the difference is that in the past there have been more then one object.
any ideas?
First of all your response is JSONObject not JSONArray. so
JSONObject myJsonobject = JSONreturn.getJSONObject("response");
myList.clear();
myJsonobject.getString("oppdatert");
So
myJsonobject.getString("oppdatert")
this will get you, your required string.

How can i parse this json file in android studio? [duplicate]

This question already has answers here:
Android Parse JSON Array
(3 answers)
Closed 6 years ago.
how can i parse this json file because the text do not have any particular indexing names
{
"titles": [
" Thoughts On The Works Of Providence",
"\"All Is Vanity, Saith the Preacher\"",
"\"And the sins of the fathers shall be\"",
"\"Arcturus\" is his other name",
"\"By the Waters of Babylon.\"",
"\"De Gustibus--\"",
"\"Faith\" is a fine invention",
"\"Faithful to the end\" Amended",
"\"Heaven\" -- is what I cannot reach!",
"\"Heaven\" has different Signs -- to me --",
"\"Heavenly Father\" -- take to thee",
"\"Home\"",
]
}
Like this:
JSONArray array = object.getJSONArray("titles");
for(int i = 0; i < array.length(); i ++){
String title = array.getString(i);
//Do something with the string
}
You treat the JsonArray like a string array.
However in the array you provided the JSON is invalid because of the comma on the last element of the array, you need to remove this comma to have valid JSON.
It's just a simple JSON. Do it like this.
String json = "{\"titles\":[\" Thoughts On The Works Of Providence\",\"\\\"All Is Vanity, Saith the Preacher\\\"\",\"\\\"And the sins of the fathers shall be\\\"\",\"\\\"Arcturus\\\" is his other name\",\"\\\"By the Waters of Babylon.\\\"\",\"\\\"De Gustibus--\\\"\",\"\\\"Faith\\\" is a fine invention\",\"\\\"Faithful to the end\\\" Amended\",\"\\\"Heaven\\\" -- is what I cannot reach!\",\"\\\"Heaven\\\" has different Signs -- to me --\",\"\\\"Heavenly Father\\\" -- take to thee\",\"\\\"Home\\\"\"]}";
JSONObject jsonObject = new JSONObject(json);
And for accessing your items:
JSONArray jsonArray = jsonObject.getJSONArray("titles");
for(int i=0;i<jsonArray.size(); i++){
Log.d(TAG, jsonArray.getString(i));
}
You can parse this to an instanse of this class :
public class Instance{
public String[] titles;
}
but you need to remove the last comma after home. If there is no another item after the comma, it means it is not a valid JSON string.
This should work.
JSONObject jsonObject = new JSONObject(yourString);
JSONArray jsonArray= jsonObject.getJSONArray("titles");
for (int i=0; i < jsonArray.length(); i++) {
string content = jsonArray.getString(i);
....
}
Try this:
Try{
JSONArray itemArray = jsonObject.getJSONArray("titles")
for (int i = 0; i < itemArray.length(); i++) {
String value=itemArray.getString(i);
Log.e("json", i+"="+value);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I want to convert below json array into array list [duplicate]

This question already has answers here:
Converting JSONarray to ArrayList
(23 answers)
Closed 6 years ago.
This is the link to my webserivce please check out
https://drive.google.com/open?id=0BwCPG2lI_UeCcXlLUGhibTJEdWs
this is image of the android code
android code
please help me
at first you should send JSONArray Data inside JSONObject then via below codes get and set your list
public void onResponse(String response) {
try {
JSONObject responseJsonObject = new JSONObject(response);
JSONArray arrayJsonObject = responseJsonObject.getJSONArray("items");
for (int i = 0; i < arrayJsonObject.length(); i++) {
JSONObject jsonObject = arrayJsonObject.getJSONObject(i);
//Or do somthing with Array Datas
}
}
}
if your array not have name :
JSONArray jsonArray = new JSONArray(String_Result);

Android json array: how do I get and specific string? [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
I have a Json script which get data from a Mysql database and print it in different TextViews. The thing I want to know is how can I take for example only the "idE" value and print it in the "TextViewTitulo" TextView
try {
String response = "[{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar","tipo":"bar"},{"0":"1","id":"1","1":"f3n","idE":"f3n","2":"bar2","tipo":"bar2"}]"
JSONArray array;
array = new JSONArray(response);
StringBuilder sb = new StringBuilder();
for (i=0; i<array.length(); i++) {
// chain each string, separated with a new line
sb.append(array.getString(i) + "\n");
}
// display the content on textview
textViewTitulo.setText(sb.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My code works fine, but is displays the whole string of values
You have 2 objects in your JSONArray, and 1 textview to show the data in, so I'm adding append " " to separate them.
for (i=0; i<array.length(); i++) {
sb.append(array.getJSONObject(i).getString("idE"));
sb.append(" ");
//do whatever with it
}
textViewTitulo.setText(sb.toString());
Of course you can get it, you just have to iterate through the whole JSONArray. If you know the value you are looking for, you have to search for it, if you know the index - it is easier, you just have to get it. So for the first case, you could do something like:
for (int i = 0; i < array.length(); i++) {
JSONObject current = array.getJSONObject(i);
// In your case the JSONObject is more complicated
// so you need to iterate over it too
Iterator<?> keys = current.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
String value = current.get(key);
// Do something with the value
}
// Or If you know what key you are looking for, you may do
String value = current.get("idE");
// Do something with value
}

How to extract values from json array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi i am new to the android development,i want to extract values from json array can you please guide me.
Here is my json
[
{
"Id": "c0f3310b-5ec2-4af0",
"UserId": "fd83ca17-41f5-472a",
"ProfileId": "100006690",
"ProfileType": "facebook",
"ProfileDate": "/Date(1380894956000)/",
"ProfileStatus": 1
},
{
"Id": "6954433d-b78e-47b6",
"UserId": "fd83ca17-41f5-8efe",
"ProfileId": "100004492",
"ProfileDate": "/Date(1380894685000)/",
"ProfileStatus": 1,
"ProfileType": "facebook"
}
]
Thank you
Like below coding
JSONArray jObject = new JSONArray(jsoninputstring);
for (int i = 0; i < jObject.length(); i++) {
JSONObject obj = jObject.getJSONObject(i);
String name= obj.getString("Id");
String email= obj.getString("UserId");
String image= obj.getString("ProfileId");
}
Here is the tutorial for JSON parsing
http://lakyrana.blogspot.in/
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
http://androidexample.com/JSON_Parsing_-_Android_Example/index.php?view=article_discription&aid=71&aaid=95
Create new JSONArray object and iterate over it.
JSONArray arr = new JSONArray(jsonString);
for(int i=0;i<arr.length;i++){
JSONObject obj = arr.getJSONObject(i);
// read data from obj using obj.getString method.
}
JSONArray categories = responseData.getJSONArray("categories"); // your JSON array
for(int i=0; i < categories.length(); i++){
JSONObject ob = (JSONObject) categories.get(i);
ob.getString("Id");
ob.getString("UserId"); // and so on
}
I would strongly suggest you to check out JacksonParser... You can download jar files from this link and you can easily find so much example how to use it. It is the easiest and fastest way to parse json into object.
JSONArray jsonArray = new JSONArray(yourResponseString);
for(int i=0;i<jsonArray.length();i++){
JSONObject dataObject=dataArray.getJSONObject(i);
String ID=dataObject.getString("Id");
String UserID=dataObject.getString("UserId");
String ProfileID = jsonObject.getString("ProfileId");
..
}

Categories

Resources