检索json数据

时间:2014-04-25 08:27:16

标签: android json parsing arrays

我正在处理一个需要解析jsonarray的应用程序。我在base64中有我的json值,我需要解码字符串以使用解码字符串来检索数据。这是我的代码:

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

我只得到第一个JSONObject,我需要获取所有值的所有JSONObject。我不能使用getString(name),因为我的json可以有其他键。我做错了什么,为什么我只获得第一个JSONObject而不是其他JSONObject?

json类型:

[
{
"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"
},

以下是我的代码:

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=}

任何人都知道我该怎么做?

谢谢

2 个答案:

答案 0 :(得分:1)

问题:

您将所有结果放在同一个地图中。 JSONArray中的每个对象都将擦除先前对象的值,因为键是相同的。

最后,每个键只能获得一个值。

解决方案:

数组中每个JSON对象需要一个映射。例如,您可以使用地图的列表(或数组)。这是一些代码:

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

答案 1 :(得分:0)

将json值添加到模型对象中我希望它对您有所帮助。

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

然后将值设置解析为bean对象之后。

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