数组列表丢失特定对象的值

时间:2019-11-27 13:56:21

标签: android arrays json

我创建了一个模型类。其中具有自己类的数组列表。因为我需要获取无限的json对象,但是当我将该数组存储在列表中并在获得特定对象后显示错误的值。如何管理这种类型的数组列表。

这是我的模型类示例:

 public class Search {
    String Value1="",Value2="";
    ArrayList<Search> searchArrayList;
}

这是我的json示例:其中items数组具有无限数组。它是动态结束级别。如果有任何子项有子项,则items []数组会出现。

"data": [
    {
        "QDetailId": "",
        "QuestionId": "1",
        "LabelName": "Airtraffic Controller's Role",
        "SequenceNo": "1",
        "QueAnswer": "",
        "QType": "Question",
        "ScreenName": "Scroll_single_selection",
        "items": [
            {
                "QDetailId": "1",
                "QuestionId": "1",
                "TechId": "1",
                "SequenceNo": "1",
                "QueAnswer": "Flight dispatch",
                "QType": "Answer",
                "ScreenName": "",
                "items": [
                    {
                        "QDetailId": "1",
                        "QuestionId": "2",
                        "LabelName": "Flight Dispatch Training",
                        "SequenceNo": "2",
                        "QueAnswer": "",
                        "QType": "Question",
                        "ScreenName": "Tab_multi_selection",
                        "items": [
                            {
                                "QDetailId": "7",
                                "QuestionId": "2",
                                "TechId": "1",
                                "SequenceNo": "2",
                                "QueAnswer": "ICAO 7192 syllabus",
                                "QType": "Answer",
                                "ScreenName": ""
                            },
                            {
                                "QDetailId": "8",
                                "QuestionId": "2",
                                "TechId": "1",
                                "SequenceNo": "2",
                                "QueAnswer": "FAA Airmen certificate - flight dispatcher",
                                "QType": "Answer",
                                "ScreenName": ""
                            }
                        ]
                    }
                ]
            },

这是我如何从API获取值的方法:

for (int i = 0; i < jsonDataArray.length(); i++) {

                                    JSONObject jsonObjSearch = jsonDataArray.getJSONObject(i);

                                    SearchTemp searchTemp = new SearchTemp();
                                    searchTemp.setQDetailId(jsonObjSearch.getString("QDetailId"));
                                    searchTemp.setQuestionId(jsonObjSearch.getString("QuestionId"));
                                    searchTemp.setLabelName(jsonObjSearch.getString("LabelName"));
                                    searchTemp.setSequenceNo(jsonObjSearch.getString("SequenceNo"));
                                    searchTemp.setQueAnswer(jsonObjSearch.getString("QueAnswer"));
                                    searchTemp.setQType(jsonObjSearch.getString("QType"));
                                    searchTemp.setScreenName(jsonObjSearch.getString("ScreenName"));

                                    if (jsonObjSearch.has("items")) {
                                        searchTemp.setSearchTempArrayList(getChildValues(jsonObjSearch));
                                    }


                                    searchFilterArrayList.add(searchTemp);
                                }


ArrayList<SearchTemp> getChildValues(JSONObject jsonObjSearch) {
    ArrayList<SearchTemp> arrayOptions = new ArrayList<SearchTemp>();

    try {
        JSONArray jsonItemArray = jsonObjSearch.getJSONArray("items");

        if (jsonItemArray.length() > 0) {

            for (int j = 0; j < jsonItemArray.length(); j++) {
                JSONObject jsonItems = jsonItemArray.getJSONObject(j);
                SearchTemp searchChild = new SearchTemp();
                searchChild.setQDetailId(jsonItems.getString("QDetailId"));
                searchChild.setQuestionId(jsonItems.getString("QuestionId"));
                if (jsonItems.has("LabelName")) {
                    searchChild.setLabelName(jsonItems.getString("LabelName"));
                }
                searchChild.setSequenceNo(jsonItems.getString("SequenceNo"));
                searchChild.setQueAnswer(jsonItems.getString("QueAnswer"));
                searchChild.setQType(jsonItems.getString("QType"));
                searchChild.setScreenName(jsonItems.getString("ScreenName"));
                if (jsonItems.has("items")) {
                    searchChild.setSearchTempArrayList(getChildValues(jsonItems));
                }

                Utility.printLog("Child->" + j, searchChild.getLabelName() + "->" + searchChild.getQueAnswer());
                arrayOptions.add(searchChild);
            }

            Utility.printLog("tag->arrayOptions->", String.valueOf(arrayOptions.size()));
        }
        Utility.printLog("tag->", "-----------------------------");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return arrayOptions;
}

这是我的Model类:SearchTemp.java

public class SearchTemp {
String QDetailId="",QuestionId="",LabelName="",SequenceNo="",QueAnswer="",QType="",ScreenName="";
ArrayList<SearchTemp> searchTempArrayList=new ArrayList<>();
boolean isSelected=false;

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}

public String getQDetailId() {
    return QDetailId;
}

public void setQDetailId(String QDetailId) {
    this.QDetailId = QDetailId;
}

public String getQuestionId() {
    return QuestionId;
}

public void setQuestionId(String questionId) {
    QuestionId = questionId;
}

public String getLabelName() {
    return LabelName;
}

public void setLabelName(String labelName) {
    LabelName = labelName;
}

public String getSequenceNo() {
    return SequenceNo;
}

public void setSequenceNo(String sequenceNo) {
    SequenceNo = sequenceNo;
}

public String getQueAnswer() {
    return QueAnswer;
}

public void setQueAnswer(String queAnswer) {
    QueAnswer = queAnswer;
}

public String getQType() {
    return QType;
}

public void setQType(String QType) {
    this.QType = QType;
}

public String getScreenName() {
    return ScreenName;
}

public void setScreenName(String screenName) {
    ScreenName = screenName;
}

public ArrayList<SearchTemp> getSearchTempArrayList() {
    Utility.printLog("getSearchTempArrayList",String.valueOf(searchTempArrayList.size()));
    return searchTempArrayList;
}

public void setSearchTempArrayList(ArrayList<SearchTemp> searchTempArrayList) {
    this.searchTempArrayList = searchTempArrayList;

    Utility.printLog("setSearchTempArrayList",String.valueOf(searchTempArrayList.size()));
}

}

2 个答案:

答案 0 :(得分:0)

使用帮助器功能,以便您可以随时递归调用

function populateSearchTemp(SearchTemp searchTemp, JSONArray jsonDataArray)
    for (int i = 0; i < jsonDataArray.length(); i++) { 
    JSONObject jsonObjSearch = jsonDataArray.getJSONObject(i);
    searchTemp.setQDetailId(jsonObjSearch.getString("QDetailId"));                                
    searchTemp.setQuestionId(jsonObjSearch.getString("QuestionId"));
    searchTemp.setLabelName(jsonObjSearch.getString("LabelName"));
    searchTemp.setSequenceNo(jsonObjSearch.getString("SequenceNo"));
    searchTemp.setQueAnswer(jsonObjSearch.getString("QueAnswer"));
    searchTemp.setQType(jsonObjSearch.getString("QType"));
    searchTemp.setScreenName(jsonObjSearch.getString("ScreenName"));

    if (jsonObjSearch.has("items"))  
       populateSearchTemp(searchTemp.getSearchTempArrayList(), 
       jsonObjSearch.getArray("items"));  

   }
}

使用助手

  SearchTemp _st = new SearchTemp();
  JSONArray initialArr = ...
  populateSearchTemp(_st, initialArr); 
  // At this point _st should have all data

答案 1 :(得分:0)

使用GSON,更改了模型类SearchTemp

  public class SearchTemp {

    @SerializedName("QDetailId")
    String QDetailId;
    @SerializedName("QuestionId")
    String QuestionId;
    @SerializedName("LabelName")
    String LabelName;
    @SerializedName("SequenceNo")
    String SequenceNo;
    @SerializedName("QueAnswer")
    String QueAnswer;
    @SerializedName("QType")
    String QType;
    @SerializedName("ScreenName")
    String ScreenName;
    @SerializedName("items")
    List<SearchTemp> items = new ArrayList<SearchTemp>();
   //getters and setters
}

ArrayList<SearchTemp> searchTempList = new ArrayList<SearchTemp>();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(arg0.toString()).getAsJsonArray();

for(JsonElement obj : jArray )
{
    SearchTemp  searchTemp = gson.fromJson( obj , SearchTemp .class);
    searchTempList.add(searchTemp);
}

然后使用另一个模型类名称,例如Category

public class Category {
  String QDetailId;
  String QuestionId;
  String LabelName;
  String SequenceNo;
  String QueAnswer;
  String QType;
  String ScreenName;

 //getters and setters
}

在您的主要班级

public static ArrayList<SearchTemp> getSearchTempTreeBreakDown(JSONArray arg0) throws JSONException {

    ArrayList<SearchTemp> searchTempList = new ArrayList<SearchTemp>();
    for(int i = 0; i < arg0.length(); i++){
        JSONObject jsonCategoryObject = arg0.getJSONObject(i);
        SearchTemp searchTemp = new SearchTemp();

        String qDetailId = jsonCategoryObject.getString("QDetailId");
        String questionId = jsonCategoryObject.getString("QuestionId");
        String labelName = jsonCategoryObject.getString("LabelName");
        String sequenceNo = jsonCategoryObject.getString("SequenceNo");
        String queAnswer = jsonCategoryObject.getString("QueAnswer");
        String qType = jsonCategoryObject.getString("QType");
        String screenName = jsonCategoryObject.getString("ScreenName");

        searchTemp.setQDetailId(qDetailId);
        searchTemp.setQuestionId(questionId);
        searchTemp.setLabelName(labelName);
        searchTemp.setSequenceNo(sequenceNo);
        searchTemp.setQueAnswer(queAnswer);
        searchTemp.setQType(qType);
        searchTemp.setQType(screenName);

            //this part should happen over and over again if there is a category[]
            JSONArray categoryArray = new JSONArray(jsonCategoryObject.getString("items"));

            for (int x = 0; x < categoryArray.length(); x++){

                JSONObject jsonProductObject1 = categoryArray.getJSONObject(x);
                // Category is model with the getters and setters method.Please see below class
                Category category = new Category();

                category.setQDetailId(jsonProductObject1.getString("QDetailId"));
                category.setQuestionId(jsonProductObject1.getString("QuestionId"));
                category.setLabelName(jsonProductObject1.getString("LabelName"));
                category.setSequenceNo(jsonProductObject1.getString("SequenceNo"));
                category.setQueAnswer(jsonProductObject1.getString("QueAnswer"));
                category.setQType(jsonProductObject1.getString("QType"));
                category.setScreenName(jsonProductObject1.getString("ScreenName"));

                searchTemp.getItems().add(category);

            }

        searchTempList.add(searchTemp);
    }
    return searchTempList;
}

在每个循环中都使用a,而不是for(;;)

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

How does the Java 'for each' loop work?

https://code.google.com/p/google-gson/

对于命名差异(根据webservice中的变量),可以使用@SerializedName之类的注释。 (因此无需使用Serializable)