Fcps
Fcps

Reputation: 345

Retrieve json data

I am working on an app which I need to parse jsonarray. I have my json values in base64 and I need to decode strings to retrive data with decode String. Here is my code :

 private class DecodeData extends AsyncTask<String, Void, String> {

    @SuppressWarnings("unchecked")
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String response = params[0];
        String keys = "";
        String value = "";
        String b64Value = "";
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        try {
            JSONArray array = new JSONArray(response);
            for (int i = 0; i < array.length(); i++) {
            Iterator<String> it = array.getJSONObject(i).keys();
                while (it.hasNext()) {
                    keys = (String)it.next();
                    value = (String)array.getJSONObject(i).get(keys);
                    b64Value = Base64.DecodeStrToStr(value);                        

                    Log.i("ASYNC TASK VALUE", b64Value);
                    map.put(keys, b64Value);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map.toString();
    }

I get only the first JSONObject and I need to get all JSONObject with all values. I can't use getString(name) because my json can have others keys. What am I doing wrong and why am I getting only the first JSONObject and not the others ?

json type :

[
{
"value": "ZG1WdVpISmxaR2tnTWpVZ1lYWnlhV3c4WW5JZ0x6NEtSMVZaUVU1QklFRk1UQ0JUVkVGUw==",
"date_create": "MjAxNC0wNC0yNSAwMDowMDowMA==",
"picture": "aHR0cDovL3dzLmFwcHMtcGFuZWwubmV0L2RhdGEvcGFsYWNpby8yNWF2cmlsLmpwZw==",
"link": "",
"title": "MjVhdnJpbA==",
"media": "",
"id_news": "MTA5NjI0",
"id_reference": "",
"type": "",
"id_categorie": "",
"date_event": "MjAxNC0wNC0yNSAwMDowMDowMA==",
"chapo": "",
"auteur": "",
"value_out": "dmVuZHJlZGkgMjUgYXZyaWxHVVlBTkEgQUxMIFNUQVI="
},
{
"value": "YzJGdFpXUnBJREkySUdGMmNtbHNQR0p5SUM4K0NrMUJVbFpKVGlCaGJtUWdSbEpKUlU1RVV3PT0=",
"date_create": "MjAxNC0wNC0yNiAwMDowMDowMA==",
"picture": "aHR0cDovL3dzLmFwcHMtcGFuZWwubmV0L2RhdGEvcGFsYWNpby8yNmF2cmlsMi5qcGc=",
"link": "",
"title": "MjZhdnJpbA==",
"media": "",
"id_news": "MTA5NjMx",
"id_reference": "",
"type": "",
"id_categorie": "",
"date_event": "MjAxNC0wNC0yNiAwMDowMDowMA==",
"chapo": "",
"auteur": "",
"value_out": "c2FtZWRpIDI2IGF2cmlsTUFSVklOIGFuZCBGUklFTkRT"
},

Here is what i am getting with my code :

RESPONSE :{date_create=MjAxNC0wNS0yNSAwMDowMDowMA==, link=, date_event=MjAxNC0wNS0yNSAwMDowMDowMA==, type=, value_out=ZGltYW5jaGUgMjUgbWFpRE9MQSBNSVpJSyBlbiBjb25jZXJ0, picture=aHR0cDovL3dzLmFwcHMtcGFuZWwubmV0L2RhdGEvcGFsYWNpby8yNW1haS5qcGc=, title=MjUgbWFp, id_reference=, chapo=, value=WkdsdFlXNWphR1VnTWpVZ2JXRnBQR0p5SUM4K0NqeGljaUF2UGdwRVQweEJJRTFKV2tsTElHVnVJR052Ym1ObGNuUT0=, id_news=MTA5NjM0, media=, auteur=, id_categorie=}

Anybody has an idea of how I can do ?

Thank you

Upvotes: 1

Views: 192

Answers (2)

Joffrey
Joffrey

Reputation: 37660

Problem:

You are putting all results in the same Map. Each object in the JSONArray will erase the values of previous objects because the keys are the same.

In the end, you get only one value for each key.

Solution:

You need one map per JSON object in the array. You could use a list (or array) of Maps, for instance. Here is some code:

ArrayList<HashMap<String, String>> decodedArray = new ArrayList<>();
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
    HashMap<String, String> map = new HashMap<>();
    Iterator<String> it = array.getJSONObject(i).keys();
    while (it.hasNext()) {
        keys = (String) it.next();
        value = (String) array.getJSONObject(i).get(keys);
        b64Value = Base64.DecodeStrToStr(value);                        
        Log.i("ASYNC TASK VALUE", b64Value);
        map.put(keys, b64Value);
    }
    decodedArray.add(map);
}

Upvotes: 1

Ajit Kumar Dubey
Ajit Kumar Dubey

Reputation: 1563

Add you json value into model object I hope it would be helpful for you.

public class A {
private Vector<B> b = new Vector<B>();
public Vector<B> getB() {
return b;
}

public void addB(B b) {
this.b.add(b);
}

public class B{
   private String value;
   private String picture;
 //add your paramatere here like link, title etc
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}
public String getPicture() {
    return picture;
}
public void setPicture(String picture) {
    this.picture = picture;
}
}
}

Then after parsing value set into bean object like that.

A a = new A();
for (int i = 0; i < jsonArray.length(); i++) {
                B b= a.new       B();
                JSONObject jsonObject= (JSONObject)jsonArray.get(i);
                String value= jsonObject.getString("value");
                jsonObject.getString("picture");
            // get all value from json object set into b object 
                a.addB(b);
            }

Upvotes: 0

Related Questions