I've a JSONArray and I need to get the hashmap with the values, because I need to populate a stream, like twitter done.
What you suggest to do?
HashMap<String, String> pairs = new HashMap<String, String>();
for (int i = 0; i < myArray.length(); i++) {
JSONObject j = myArray.optJSONObject(i);
Iterator it = j.keys();
while (it.hasNext()) {
String n = it.next();
pairs.put(n, j.getString(n));
}
}
Something like that.
You can use Iterator for getting JsonArrays. or use this way
eg. json
{
........
........
"FARE":[ //JSON Array
{
"REG_ID":3,
"PACKAGE_ID":1,
"MODEL_ID":9,
"MIN_HOUR":0
.......
.......
.......
}
]
}
HashMap<String, String> mMap= new HashMap<>();
for (int i = 0; i < myArray.length(); i++) {
JSONObject j = myArray.optJSONObject(i);
mMap.put("KEY1", j.getString("REG_ID"));
mMap.put("KEY2", j.getString("PACKAGE_ID"));
............
............
}
note: For better coding use Iterator
Related
i have this code..
JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");
cartarraylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
HashMap<String, String> lmap = new HashMap<String, String>();
JSONObject p = jsonarrayCartProducts.getJSONObject(i);
// Retrive JSON Objects
lmap.put("products_id", p.getString("products_id"));
lmap.put("products_name", p.getString("products_name"));
lmap.put("products_price", p.getString("products_price"));
lmap.put("products_image", p.getString("products_image"));
lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
lmap.put("products_price_total", p.getString("products_price_total"));
lmap.put("pcustomersid", customersid);
lmap.put("pcountryid", countryid);
lmap.put("customers_basket_id", p.getString("customers_basket_id"));
// Set the JSON Objects into the array
cartarraylist.add(lmap);
}
i want to sum the quantity p.getString("customers_basket_quantity") and set to my textview..
i tried to do create an
int qtySum=0;
int qtyNum;
and do this inside for loop..
qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
qtySum += qtyNum;
and set qtySum to my textview
textTotalitems.setText(qtySum);
but i got error, the app crashed..
this is updated code with sum i tried..
JSONObject jsonObjcart = new JSONObject(myJSONCartProducts);
jsonarrayCartProducts = jsonObjcart.getJSONArray("cartproducts");
cartarraylist = new ArrayList<HashMap<String, String>>();
int qtySum=0;
int qtyNum;
for (int i = 0; i < jsonarrayCartProducts.length(); i++) {
HashMap<String, String> lmap = new HashMap<String, String>();
JSONObject p = jsonarrayCartProducts.getJSONObject(i);
// Retrive JSON Objects
lmap.put("products_id", p.getString("products_id"));
lmap.put("products_name", p.getString("products_name"));
lmap.put("products_price", p.getString("products_price"));
lmap.put("products_image", p.getString("products_image"));
lmap.put("customers_basket_quantity", p.getString("customers_basket_quantity"));
lmap.put("products_price_total", p.getString("products_price_total"));
lmap.put("pcustomersid", customersid);
lmap.put("pcountryid", countryid);
lmap.put("customers_basket_id", p.getString("customers_basket_id"));
// Set the JSON Objects into the array
qtyNum = Integer.parseInt(p.getString("customers_basket_quantity"));
qtySum += qtyNum;
cartarraylist.add(lmap);
}
textTotalitems.setText(qtySum);
Use
textTotalitems.setText(String.valueOf(qtySum));
instead of
textTotalitems.setText(qtySum);
With your current implementation your trying to set a resource-Id to your TextView, because TextView has an overloaded setText(int resId)-method.
I'm trying to get elements from my json array.
this is my json response:
{"IDs":["635426812801493839","635429094450867472","635433640807558204"]}
This is what I've tried so far:
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
JSONObject obj = a.getJSONObject(i);
stringArray.add(obj.toString());
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());
But I'm getting empty list. What is wrong with my code? Any help will be truly appreciated. Thanks.
The for loop should be
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
your the JSONArray contains already string
JSONObject obj = a.getJSONObject(i);
replace with
String str = a.getString(i);
Use this
itemList = new ArrayList<HashMap<String, String>>();
JSONArray a = jsonObj.getJSONArray(Constants.IDS);
int arrSize = a.length();
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0; i < arrSize; ++i) {
stringArray.add(a.getString(i));
item = new HashMap<String, String>();
item.put(Constants.ID, obj.toString());
itemList.add(item);
}
Log.e("ARR COUNT", "" + stringArray.size());
i am having a issue of parsing JSON data i followed this link..
over there it has parsing data images description..
and i also refereed this in stack over flow a guy who is having same issue but no correct answer..
How to Parse JSONarray inside JSONarray in android? and
How to display Image from URL?
we can say just extention of the above questions..
its not a duplicate that guy is also same problem no answer..
i have a data like below
{
"request": "ok",
"query": {
"result": [
{
"site": [
{
"latest": [
{
"id": "2eaQy8Ow",
"data": "1/1/2014"
}
]
}
],
"flag": [
"http://www.simplydecoded.com/wp-content/uploads/2013/02/Telangana2.jpg"
]
}
]
}
}
i am using below code for parsing
JSONArray json_query_flag = c.getJSONArray("flag");
JSONArray json_query_site=c.getJSONArray("site");
System.out.println("looping json_query_site");
for (int j = 0; j < c.length(); j++) {
System.out.println("looping json_query_site[" + j +"]" + "json_query_site.length() -->" + json_query_site.length());
if (j <json_query_site.length()) {
HashMap<String, String> map1 = new HashMap<String, String>();
JSONObject sd = json_query_site.getJSONObject(j);
// get latestoffers
JSONArray json_latest = sd.getJSONArray("latest");
System.out.println(json_latest.toString());
for (int k = 0; k < json_latest.length(); k++) {
HashMap<String, String> map2 = new HashMap<String, String>();
JSONObject e = json_latest.getJSONObject(k);
My problem is that i am not getting the latest and flag..
may be parsing problem..
Hellow man thanks for understanding the problem please follow this post for the till your site and latest..
it solved I am getting text.. but problem with images I think you have flag..
for that you need to change your Listviewadapter.java file. so that images will appear
change this like
String strflag = resultp.get(Mainactivity.IMAGES);
if(strflag != null)
imageLoader.DisplayImage(strflag, flag);
else
imageLoader.DisplayImage("http://www.butterentals.com/graphics/no_image.jpg", flag);
so that I will be done.
TRy this..
JSONObject JObj = new JSONObject(response);
JSONObject query = JObj.getJSONObject("query");
JSONArray result = query.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject jobj = result.getJSONObject(i);
JSONArray site = jobj.getJSONArray("site");
for (int j = 0; j < site.length(); j++) {
JSONObject sitobj = site.getJSONObject(j);
JSONArray latest = sitobj.getJSONArray("latest");
for (int k = 0; k < latest.length(); k++) {
JSONObject lateobj = latest.getJSONObject(k);
System.out.println("id : "+lateobj.getString("id"));
}
}
JSONArray flag = jobj.getJSONArray("flag");
for (int l = 0; l < flag.length(); l++) {
System.out.println("urls : "+flag.getString(l));
}
}
I'm trying to parse a json inside an HashMap to use the data inside my app.
Here is a simplified version of the code:
JSONArray array1 = new JSONArray(json);
for (int i = 0; i < array1.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject object1 = array1.getJSONObject(i);
JSONObject object2 = object1.getJSONObject("job");
JSONArray array2 = object2.getJSONArray("formations");
if (array2.length() != 0) {
for (int i1 = 0; i1 < array2.length() - 1; i1++) {
JSONObject object3 = array2.getJSONObject(i1);
map.put("formation-created_at",
}
}
map.put("testimony", object2.getString("testimony"));
JSONArray array3 = object2.getJSONArray("themes");
if (array3.length() != 0) {
for (int i2 = 0; i2 < array3.length() - 1; i2++) {
JSONObject object4 = array3.getJSONObject(i2);
map.put("themes-intro", object4.getString("intro"));
}
}
JSONObject object5 = object2.getJSONObject("sector");
map.put("sector-description", object5.getString("description"));
list.add(map);
}
Log.d("testimony", list.get(1).get("testimony"));
Log.d("themes-intro", list.get(1).get("themes-intro"));
I can recover the first object in the map with :
Log.d("testimony", list.get(1).get("testimony"));
But i cannot recover the one who is inside the loop :
Log.d("themes-intro", list.get(1).get("themes-intro"));
The logcat return the error :
FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:138)
I know that I should specify which "themes-intro" I want to call from the loop, but I cannot add a .get(x) to the expression.
Edit :
Here is the full json : http://komfushee.com/documents/ctai/jobs.json
And I've edited my code to give the full version of the code
please use below code i think i hope it will be haplfull....
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
if (array1.length() != 0) {
for (int i = 0; i < array1.length(); i++) {
JSONObject object1 = array1.getJSONObject(i);
JSONObject object2 = object1.getJSONObject("job");
map1.put("testimony", object2.getString("testimony"));
}
}
if (array2.length() != 0) {
for (int i1 = 0; i1 < array2.length() - 1; i1++) {
JSONObject object3 = array2.getJSONObject(i1);
map2.put("formation-created_at", object3.getString("created_at"));
}
}
I have JSONArray and when I decode JSON to HashMap at that time HashMap take last value of JSONArray.
here my code:
QjArray = new JSONArray(Questionresult);
JSONObject json_data = new JSONObject();
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
json_data = QjArray.getJSONObject(i);
jQuestionName =json_data.getString("QuestionName");
objJMap.put("QuestionName",jQuestionName);
jQuestiontypeid = json_data.getInt("Questiontypeid");
String Qid = jQuestiontypeid.toString();
objJMap.put("Questiontypeid", Qid);
jAnswertypeid = json_data.getInt("Answertypeid");
String Aid = jAnswertypeid.toString();
objJMap.put("Answertypeid", Aid);
}
My JSONArray:
This is question list[{"QuestionID":"1","QuestionName":"when you come","Questiontypeid":"1","Answertypeid":"1"},{"QuestionID":"2","QuestionName":"about your words","Questiontypeid":"1","Answertypeid":"2"},{"QuestionID":"3","QuestionName":"you want extra service?","Questiontypeid":"1","Answertypeid":"3"},{"QuestionID":"4","QuestionName":"performance of quality ?","Questiontypeid":"1","Answertypeid":"4"},{"QuestionID":"5","QuestionName":"performance of staff?","Questiontypeid":"1","Answertypeid":"5"},{"QuestionID":"6","QuestionName":"when you left room ?","Questiontypeid":"2","Answertypeid":"1"},{"QuestionID":"7","QuestionName":"your words about roomservice ?","Questiontypeid":"2","Answertypeid":"2"},{"QuestionID":"8","QuestionName":"you like roomservice ?","Questiontypeid":"2","Answertypeid":"3"},{"QuestionID":"9","QuestionName":"performance room service ?","Questiontypeid":"2","Answertypeid":"4"},{"QuestionID":"10","QuestionName":"performance room service staff?","Questiontypeid":"2","Answertypeid":"5"}]
I think there are certain problems in your logic. For every JSON object u are creating new HashMap object inside for loop, so u will loose any previous data. Also HashMap will override new Data, so you will have only final data. What u can do is that create an arrayList of Hashmap....
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<STring, String>>();
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
........
data.add(objJMap);
...}
This is because your code re initializes the HashMap after every loop.
for (int i = 0; i<QjArray.length(); i++) {
objJMap = new HashMap<String, String>();
....
}
Place the HashMap outside the loop and it will work fine.
objJMap = new HashMap<String, String>();
for (int i = 0; i<QjArray.length(); i++) {
....
}