如何使用对象数组获取JSONObject

时间:2016-01-04 12:32:35

标签: java json

我有json字符串数据,如:

ImageButton imageButton;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Find your views in onCreate();
            imageButton = (ImageButton) findViewById(R.id.imageWinkelmand); 

    }

我想获取所有数据意味着id,mobile和所有名称.. 所以,我有Contact Class:

 {"contactlist":["{"id":"1","mobile":"186010855","name":"Intex"}","{"id":"212","mobile":"980067","name":"pari"}"]}

并且我已尝试过抓取:

public class Contact {
    private String id;
    private String name;
    private String mobile;
    //getters and settrs & constructors
    }

但是我无法获取这样的数据..我可以获取这个json数据吗?

4 个答案:

答案 0 :(得分:2)

根据@ShubhamChaurasia的评论,你的JSON无效。以下JSON验证:

{
    "contactlist": [{
        "id": "1",
        "mobile": "186010855",
        "name": "Intex"
    },
    {
        "id": "212",
        "mobile": "980067",
        "name": "pari"
    }]
}

在已经显示的Java中使用\"来逃避这一点,这将停止您的JSON验证错误。我建议将它与@Joopkins'或@JanTheGun的答案结合起来。

我用于JSON验证的好网站是http://jsonlint.com/

答案 1 :(得分:1)

您需要转义双引号才能获得有效的json字符串。然后你可以像这样读取json数组:

String stringdata = "{\"contactlist\":[{\"id\":\"1\",\"mobile\":\"186010855\",\"name\":\"Intex\"},{\"id\":\"212\",\"mobile\":\"980067\",\"name\":\"pari\"}]}";

JsonElement jsonElement = new JsonParser().parse(stringdata);
JsonObject jsonOject = jsonElement.getAsJsonObject();
JsonArray jsonArray = jsonOject.getAsJsonArray("contactlist");

for (JsonElement element : jsonArray) {
    JsonObject obj = element.getAsJsonObject();

    String id = obj.get("id").toString();
    String name = obj.get("name").toString();
    String mobile = obj.get("mobile").toString();

    Contact contact = new Contact(id, name, mobile);
    System.out.println("id: " + contact.getId());
    System.out.println("name: " + contact.getName());
    System.out.println("mobile: " + contact.getMobile());
    System.out.println("");
}

答案 2 :(得分:1)

您可以将stringdata转换为包含每个联系人的JSONObject的JSONArray。

    JSONObject contactData = new JSONObject(stringdata);
    JSONArray contactArray = contactData.getJSONArray("contactlist");
    //Create an array of contacts to store the data
    Contact[] contacts = new Contact[contactArray.length()];
    //Step through the array of JSONObjects and convert them to your Java class
    Gson gson = new Gson();
    for(int i = 0; i < contactArray.length(); i++){
         contacts[i] = gson.fromJson(
              contactArray.getJSONObject(i).toString(), Contact.class);
          }

现在,您有一个来自原始JSON数据的联系人数组。

答案 3 :(得分:0)

#include <iostream>
#include <string>

struct Base {
    virtual ~Base() = default;
    virtual void id(){
        std::cout << id_ << std::endl;
    }
    std::string id_ = "Base";
};   



struct Derived : public Base {
    virtual ~Derived() = default;
    std::string id_ = "Derived";
};   


int main(){
    Base* b = new Derived();
    Derived* d = new Derived();
    b->id();
    d->id();
    delete d;
    delete b;                                                                                                          
    return 0;
}