Android 从 java WS 解析 JsonArray 到 hashmap

Android Parsing JsonArray to hashmap from java WS

我正在尝试从 http 请求中获取 json 数组,我正在学习本教程:

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

我有疑问,在教程中 json 文件是这样的:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c2012",
                "name": "Eminem",
                "email": "eminem@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}

这是我的 json:

[
{"email":"prova","one":"test","data":"2015-02-10 00:00:00.0","cash":100},
{"email":"prova","one":"provadue","data":"2015-02-11 23:41:32.0","cash":15}
]

这是我的 "contact" class Web 服务:

public class contacts
{
private String email;
private String one;
private String data;
private int cash;

public contacts()
{

}

public contacts(String email, String one, String data, int cash)
{
super();
this.email = email;
this.one = one;
this.data = data;;
this.cash = cash;
}


public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getOne() {
    return one;
}

public void setOne(String one) {
    this.one = one;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public int getCash() {
    return cash;
}

public void setCash(int cash) {
    this.cash = cash;
}

@Override
public String toString()
{
return "Contact [email=" + email + ", one=" + one + ", data=" + data + ", cash=" + cash + "]";
}

}

现在跟随教程有那部分:

// Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

我以为这是指:

{
    "contacts": [

但是在我的 Json 中我没有那个节点,我可以做些什么来更改教程脚本或更改我的 json 来自 Web 服务的响应?

提前致谢!

尝试使用这一行:

contacts = jsonObj.getJSONArray(0);

但是,您应该始终向 JSON 字符串添加一个键(就像教程示例中的那个 "contacts" 键)并使用:

contacts = jsonObj.getJSONArray("contacts");

得到你的 JSON 阵列。

编辑:好的,所以我创建了一个示例,说明如何使用 contacts class 获取数组的密钥。我无法实现您在示例中放入的 WebService,但这应该让您了解如何获取密钥...

您的 contacts class 完好无损。

我创建了一个名为 ContactsObject 的新 class:

import java.util.ArrayList;

public class ContactsObject {

    private ArrayList<contacts> contacts;   // THIS IS HOW YOUR KEY WILL BE NAMED (in this case, "contacts")

    public void setListContacts(ArrayList<contacts> contacts)
    {
        this.contacts = contacts;
    }

    public ArrayList<contacts> getListContacts()
    {
        return this.contacts;
    }
}

然后在MainActivity,(在onCreate,但这并不重要)我添加了这些代码行来测试所有内容:

        ContactsObject objj = new ContactsObject();

        ArrayList<contacts> moreContacts = new ArrayList<contacts>();

        contacts cnt = new contacts();
        cnt.setCash(100);
        cnt.setData("some data");
        cnt.setEmail("some mail");
        cnt.setOne("one?");
        moreContacts.add(cnt);

        contacts cnt1 = new contacts();
        cnt1.setCash(100);
        cnt1.setData("some data1");
        cnt1.setEmail("some mail1");
        cnt1.setOne("2?");
        moreContacts.add(cnt1);

        contacts cnt2 = new contacts();
        cnt2.setCash(100);
        cnt2.setData("some data2");
        cnt2.setEmail("some mail2");
        cnt2.setOne("3?");
        moreContacts.add(cnt2);  
        // "moreContacts" is similar to that example's "donationList", I think...

        objj.setListContacts(moreContacts);  // doing this to set the "key"

        Gson gson = new Gson();
        Log.w("READ THIS!!!", gson.toJson(objj));  // check your Log for warning "READ THIS!!!", this should be printed out:
        // {"contacts":[{"data":"some data","email":"some mail","one":"one?","cash":100},{"data":"some data1","email":"some mail1","one":"2?","cash":100},{"data":"some data2","email":"some mail2","one":"3?","cash":100}]}

EDIT2:这就是您应该尝试使代码看起来像的方式(与您提供的示例相比):

        String donations = null;
        ContactsObject objj = new ContactsObject();
        ArrayList<myDonations> donationList = new ArrayList<myDonations>();

        try
        {
            donationList = new DonationManager().getList();  // or however you get the array list
            objj.setListContacts(donationList);
            Gson gson = new Gson();
            donations = gson.toJson(objj);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

希望这对您有所帮助。