Android将JSON数据解析为哈希表?

Android将JSON数据解析为哈希表?,android,Android,我从web服务获取数据作为JSON数据。显示的数据如图所示:它基本上是一个名为questionly的类,它有两个属性,即QuestionnaireId和QuestionnaireName。因此,我调用的web服务方法返回这个类的集合 我正在尝试将这些数据解析为一个哈希表,但我无法解析。请帮助我将此数据解析为哈希表 您将需要与以下内容类似的内容: Hashtable<Integer,String> table = new Hashtable<Integer,String>(

我从web服务获取数据作为JSON数据。显示的数据如图所示:它基本上是一个名为questionly的类,它有两个属性,即QuestionnaireId和QuestionnaireName。因此,我调用的web服务方法返回这个类的集合


我正在尝试将这些数据解析为一个哈希表,但我无法解析。请帮助我将此数据解析为
哈希表

您将需要与以下内容类似的内容:

Hashtable<Integer,String> table = new Hashtable<Integer,String>();

/* ... */

JsonObject root = new JsonObject(json_string);
JsonArray questions = root.getJsonArray("d");
for(int i = 0; i < questions.length(); i++) {
    JsonObject question = questions.getJsonObject(i);
    int id = question.optInt("QuestionnaireId", -1);
    String name = question.optString("QuestionnaireName");
    table.put(id, name);
}
Hashtable=newhashtable();
/* ... */
JsonObject root=新的JsonObject(json_字符串);
JsonArray questions=root.getJsonArray(“d”);
对于(int i=0;i

(大致…

您将需要以下内容:

Hashtable<Integer,String> table = new Hashtable<Integer,String>();

/* ... */

JsonObject root = new JsonObject(json_string);
JsonArray questions = root.getJsonArray("d");
for(int i = 0; i < questions.length(); i++) {
    JsonObject question = questions.getJsonObject(i);
    int id = question.optInt("QuestionnaireId", -1);
    String name = question.optString("QuestionnaireName");
    table.put(id, name);
}
Hashtable=newhashtable();
/* ... */
JsonObject root=新的JsonObject(json_字符串);
JsonArray questions=root.getJsonArray(“d”);
对于(int i=0;i

(大概…

我用这个方法解析了一些json字符串,希望对您有所帮助

public static Vector<MyObject> getImagesFromJson(String jos){
        Vector<MyObject> images = null;
        try{

            JSONObject jo = new JSONObject(jos);

            JSONArray array = jo.getJSONArray("images");
            if(array != null && array.length() > 0){
                images = new Vector<MyObject>();
            }
            for (int i=0;i<array.length();i++) {
                MyObject object = new MyObject();
                object.setEpigrafe( array.getJSONObject( i ).get( "epigrafe" ).toString() );
                object.setId( array.getJSONObject( i ).getInt( "id" ) );
                object.setUrl( array.getJSONObject( i ).get( "url" ).toString() );
                images.add(object);
            }
        }catch(Exception e){
            images = null;
            e.printStackTrace();
        }
        return images;
    }

where MyObjects is defined as:

    private int id;
    private String url;
    private String epigrafe;
公共静态向量getImagesFromJson(字符串jos){
向量图像=空;
试一试{
JSONObject jo=新的JSONObject(jo);
JSONArray数组=jo.getJSONArray(“图像”);
if(array!=null&&array.length()>0){
图像=新向量();
}

对于(inti=0;i我用这个方法解析一些json字符串,希望对您有所帮助

public static Vector<MyObject> getImagesFromJson(String jos){
        Vector<MyObject> images = null;
        try{

            JSONObject jo = new JSONObject(jos);

            JSONArray array = jo.getJSONArray("images");
            if(array != null && array.length() > 0){
                images = new Vector<MyObject>();
            }
            for (int i=0;i<array.length();i++) {
                MyObject object = new MyObject();
                object.setEpigrafe( array.getJSONObject( i ).get( "epigrafe" ).toString() );
                object.setId( array.getJSONObject( i ).getInt( "id" ) );
                object.setUrl( array.getJSONObject( i ).get( "url" ).toString() );
                images.add(object);
            }
        }catch(Exception e){
            images = null;
            e.printStackTrace();
        }
        return images;
    }

where MyObjects is defined as:

    private int id;
    private String url;
    private String epigrafe;
公共静态向量getImagesFromJson(字符串jos){
向量图像=空;
试一试{
JSONObject jo=新的JSONObject(jo);
JSONArray数组=jo.getJSONArray(“图像”);
if(array!=null&&array.length()>0){
图像=新向量();
}

对于(int i=0;i你可以很容易地问谷歌:

你可以很容易地问谷歌:

我建议你不要只是把它塞进地图,而是把数据映射到POJO,然后用POJO填充列表

这样定义您的POJO

public class Questionnaire {

    private int id;
    private String name;

    public Questionnaire() {
        setId(0);
        setName(null);
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
创建
调查问卷
对象列表

现在,每次需要时都创建一个新的
调查问卷
对象,并使用setter将解析数据映射到该对象


完成一个
调查问卷
对象后,将其添加到列表中。

我建议不仅将其填充到地图中,而且将数据映射到POJO,然后用该POJO填充列表

这样定义您的POJO

public class Questionnaire {

    private int id;
    private String name;

    public Questionnaire() {
        setId(0);
        setName(null);
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
创建
调查问卷
对象列表

现在,每次需要时都创建一个新的
调查问卷
对象,并使用setter将解析数据映射到该对象

完成一个
调查问卷
对象后,将其添加到列表中