Recover multiple data from json - android

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

Related

Android Json Handling issue

I want to display updated_at data to user only if value of password in folder Array is null.
JSONArray jsonArray = object.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject dataa = jsonArray.getJSONObject(i);
JSONArray jsonArray2 = dataa.getJSONArray("folder");
for (int i2 = 0; i2 < jsonArray.length(); i2++)
{
JSONObject dataa2 = jsonArray2.getJSONObject(i2);
String password = dataa2.getString("password");
if (password.equals("null") || password.equals(null))
{
//Display data
}
else
{
// dont Display data
}
}
Is there a way to do it?
Rectify your inner loop condition section.
JSONArray jsonArray2 = dataa.getJSONArray("folder");
for (int i2 = 0; i2 < jsonArray2.length(); i2++)
{
JSONObject dataa2 = jsonArray2.getJSONObject(i2);

Android get elements from json array

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());

Android JSON getJSONObject not working, incorrect parsing

I cannot figure out why my parsing is not working, this is my JSON:
{
"fileVersion":"1.0",
"graves":[
{
"ID_grave":"1",
"ID_line":"1",
"sequence":"1",
"persons":[
{
"ID_person":"1",
"name":"Janez",
"surname":"Novak",
"dateBirth":"1956-08-11",
"dateDeath":"2014-02-12",
"important":"0",
"imp_desc":""
}
]
},
{
"ID_grave":"2",
"ID_line":"1",
"sequence":"2",
"persons":[
{
"ID_person":"2",
"name":"Mojca",
"surname":"Novak",
"dateBirth":"1953-02-13",
"dateDeath":"2012-04-08",
"important":"0",
"imp_desc":""
}
]
}
]
}
This code is working, when I want to get the first JSONObject:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
graveArrayList.add(grave.getString("ID_grave"));
}
But I would like to get the "persons" array in "graves" object. This should work but it's not, I am getting only the first persons array, where the name is Janez and not the second array where the name is Mojca:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
//The problem was because of the index i, you have to change to k and it will work
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
graves is a JSONArray and persons is a JSONArray into graves
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
}
}
}
Do your parsing as follows ,
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
Try This:
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
JSONObject grave= persons.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
//so on..
}
}
Ok only today I discovered that I am getting the data only from the first persons array and from the second where for example is a person with a name Mojca..I tried all the three given solutions but nothing works..

How to parse Json data from URL?which contains number of arrays

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

JSONArray to HashMap

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

Categories

Resources