how to parsing Json Array in android? [closed] - android

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
How to parse the below json.
[
{
"products": [
{
"description": "USB2.0. 1000 dpi, Double Lens Technology. Black
(Red,Orange.Green.Grey),White(Red,Orange.Green,Grey)
<\/p>",
"product_id": "36",
"name": "New
MP-770",
"product_code": "MO018",
"images": "productimage\/36",
"price": "59900",
"weight": "1.00"
}
],
"num_page": 1
}
]

JSONArray json = jParser.getJSONFromUrl(URL);
try {
JSONObject c = json.getJSONObject(0);
JSONArray json1 = c.getJSONArray("products");
status = c.getString("num_page");
for(int i=0;i<json1.length();i++){
JSONObject c1 = json1.getJSONObject(i);
String des = c1.getString("description");
String pid = c1.getString("product_id");
String name = c1.getString("name");
String pc = c1.getString("product_code");
String images = c1.getString("images");
String price = c1.getString("price");
String weight = c1.getString("weight");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("description", des);
map.put("product_id", pid);
map.put("name", name);
map.put("product_code",pc);
map.put("images", images);
map.put("price", price);
map.put("weight", weight);
fetch.add(map);
}
copy paste the code and use it, fetch is your arraylist of hashmap and if any problem persist then let me know.

You can do it with Android Build in JsonObject/JsonArray or you can use a library from Google called GSON
Have a look at these two Tutorials: GOOGLE and this ONE
But please use google the next time, there are thousands of tutorials explaining this to you

Related

How can I parse nested array inside JSON object using Android Volley

I want to parse below JSON format,
Can anybody please advise a way to do it? Thanks.
JSON format, I want to parse
{
"questions": [
{
"question": "What is the scientific name of a butterfly?",
"answers": [
"Apis",
"Coleoptera",
"Formicidae",
"Rhopalocera"
],
"correctIndex": 3
},
{
"question": "How hot is the surface of the sun?",
"answers": [
"1,233 K",
"5,778 K",
"12,130 K",
"101,300 K"
],
"correctIndex": 1
},
{
"question": "Who are the actors in The Internship?",
"answers": [
"Ben Stiller, Jonah Hill",
"Courteney Cox, Matt LeBlanc",
"Kaley Cuoco, Jim Parsons",
"Vince Vaughn, Owen Wilson"
],
"correctIndex": 3
}
]
}
I am trying to get it done by the below codes, It's not completed, I can parse the first array and object inside it.
public List getGeneralQuestion(final AnswerListAsyncResponse callBack){
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
URL, null, response -> {
try {
JSONArray jsonArray = response.getJSONArray("questions");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String question = jsonObject.getString("question");
int correctAnswer = jsonObject.getInt("correctIndex");
GeneralQuestions generalQuestions = new GeneralQuestions(question, correctAnswer);
generalQuestionsArrayList.add(generalQuestions);
JSONArray jsonAnsArray = jsonObject.getJSONArray("answers");
for (int j = 0; j < jsonAnsArray.length(); j++){
JSONArray jsonArrayList = jsonAnsArray.getJSONArray(j);
String answerList = jsonArrayList.getString();///////
GeneralQuestions generalAnswer = new GeneralQuestions(answerList);
generalAnsArrayList.add(generalAnswer);
}
}
Create your response model class. You can do it with an online tool or android studio plugins.
You can find a good online tool here .And about android studio plugins , Go to File -> Settings -> Plugins and search for JSON to POJO and install a good plugin and restart android studio in order to use it .
stop going through each and every JSON node since this is a simple JSON response. Use GSON for easy conversions. add GSON dependency to your app level Build.gradle file and start using GSON to convert your JSON response to a List of model class objects. It's simple and easy. You can find a good tutorial here.

How to read Json data in android [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a raw data as in a below format:
[
{
"id": "1",
"name": "abc",
"type": "consumer"
},
{
"id": "2",
"name": "cdf",
"type": "consumer"
},
{
"id": "3",
"name": "jok",
"type": "owner"
}
]
Please let me know how can I covert that into JsonArray and get the each values.
This is the simplest way to parse your JSON String. I would suggest your to read JSON documents.
But, here is a sample code.
try {
String jsonString = new String("[{\"id\": \"1\",\"name\": \"abc\",\"type\": \"consumer\"}]");
JSONArray jsonArray = new JSONArray(jsonString);
for(int index = 0;index < jsonArray.length(); index++) {
JSONObject jsonObject = jsonArray.getJSONObject(index);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String type = jsonObject.getString("type");
}
} catch (JSONException e) {
e.printStackTrace();
}
You can also parse using GSON https://code.google.com/p/google-gson/

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

Extract data from response entity [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
I want to know how to extract data from this list.
Response
[
{
"CCTID": "46204",
"Name": "Christopher Columbus",
"CarrierId": 64239
},
{
"CCTID": "46208",
"Name": "Keith Bowles",
"CarrierId": 64239
},
{
"CCTID": "46205",
"Name": "Michael Jordan",
"CarrierId": 64239
},
{
"CCTID": "46207",
"Name": "NESV PH",
"CarrierId": 64239
}
]
How can I group this data to per driver and access the following data like a list. For example. List of drivers. Driver[0]["CCTID"] returns "46204", Driver[0]["Name"] returns "Driver[0]["CCTID"] returns "46204" Driver[0]["CarrierId"] returns "64239" so on and so forth. Any ideas? Thanks!
Try this way
try {
JSONArray arr = new JSONArray(EntityUtils.toString(response.getEntity()));
for (int i = 0; i < arr.length(); i++) {
JSONObject driver = arr.getJSONObject(i);
System.out.println("CCTID : " + driver.getString("CCTID"));
System.out.println("Name : " + driver.getString("Name"));
System.out.println("CarrierId : " + driver.getString("CarrierId"));
}
} catch (Exception e) {
}

Could someone help me with parsing this JSON file?

I have this JSON file:
{
"ics_desire":{
"version":"Beta 0.1.1",
"description":"description here",
"build-fingerprint":"fingerprint"
}
Now I want to put the version part in a txtVersion textview and the description part in the txtDescription textview.
Would someone help me?
So first off to state what was said above you JSON code is incorrectly formatted. You can validate you have correctly formated JSON code by going to http://json.parser.online.fr/.
Your correct JSON would be as follows (you were missing a closing })
{
"ics_desire": {
"version": "Beta 0.1.1",
"description": "description here",
"build-fingerprint": "fingerprint"
}
}
Next, here is an example of a working JSON code that I have used to test with in the past.
{
"HealthySubstituteData": [
{
"Assoc_ID": "1",
"uFood": "White Flour",
"hFood": "Wheat Flour",
"Category": "Baking",
"Description": "You can substitute blah blah blah...",
"Count": "5",
"submittedBy": "Administrator"
}
]
}
And here is the code I use to get that data.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://your.url.com/whaterver");
try
{
JSONArray healthySubstituteData = json.getJSONArray("HealthySubstituteData");
for(int i=0;i<healthySubstituteData.length();i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = healthySubstituteData.getJSONObject(i);
map.put("Assoc_ID", (String) e.get("Assoc_ID"));
map.put("uFood", (String) e.get("uFood"));
map.put("hFood", (String) e.get("hFood"));
map.put("category", (String) e.get("Category"));
map.put("description", (String) e.get("Description"));
map.put("count", (String) e.get("Count"));
map.put("submittedBy", (String) e.get("submittedBy"));
mylist.add(map);
}
}
So now I end up with an array list of type HashMap and I can do whatever I want with it at that point.
You can create a simple class like
public class Myclass{
String version;
String description;
String build-fingerprint;
};
and then use Gson to convert json to your object and vice versa:
Gson gson = new Gson();
Myclass obj = gson.fromJson(json_string, Myclass.class);
json_string should contain your jason string.
Now you can get your class objects values using getters.
Note: Gson is an external library you need to import, download it from google.
Ignoring about the missing brackets in JSON, this code would help you through
JSONObject json = JSONfunctions.getJSONfromURL("http://your.url.com/whaterver"); //the site where you get your JSON
JSONObject daata = json.getJSONObject("ics_desire"); //grabbing the inner object
String version = daata.getString("version"); //grabbing values
String description = daata.getString("description");
txtVersion.setText(version); //Hope you have initialized the Views.
txtDescription.setText(description);

Categories

Resources