【问题标题】:How to get data from [Object] android如何从 [Object] android 获取数据
【发布时间】:2021-06-30 10:20:52
【问题描述】:

我从我的Express API 电话中收到此response

回复如下:

{
    "responseData": [{
        "unitNames": [
            "Matrices",
            "Complex Numbers"
        ],
        "subject": "maths",
        "unitTopics": {
            "1": [{
                    "topicName": "1.1 Introduction",
                    "topicURL": ""
                },
                {
                    "topicName": "1.2 Square Matrix",
                    "topicURL": ""
                }

            ],
            "2": [{
                    "topicName": "2.1 Numbers",
                    "topicURL": ""
                }

            ]
        }
    }]
}

我通过在 Android 中使用 Retrofit 得到了响应。它很好用。但它无法解析 Objects

这是我在 Android 端的问题。

{
        "responseData": [{
            "unitNames": [
                "Matrices",
                "Complex Numbers"
            ],
            "subject": "maths",
            "unitTopics": {
                "1": [[Object],
                    [Object]
    
                ],
                "2": [[Object]
    
                ]
            }
        }]
    }

它显示对象而不是我的数据。如何解决这个问题

这是代码:

System.out.println(response.body().getResponseData())
String received_data = response.body().getResponseData();
received_data_sub_units_topics_json = new JSONArray("["+received_data+"]");
System.out.println("MAIN2 "+received_data_sub_units_topics_json);

转换成jsonarray后是这样的,

{
        "responseData": [{
            "unitNames": [
                "Matrices",
                "Complex Numbers"
            ],
            "subject": "maths",
            "unitTopics": {
                "1": [["Object"],
                    ["Object"]
    
                ],
                "2": [["Object"]
    
                ]
            }
        }]
    }

请帮我解决一些问题

【问题讨论】:

    标签: java android json kotlin retrofit


    【解决方案1】:

    对于 json,我总是使用库 com.fasterxml.jackson。

    你也可以使用org.json.JSONArray、org.json.JSONObject。

    以下是每个示例: 1-杰克逊

    对于实现这个(有点长,但你会将它转换为 java 类,因此,你可以编辑值并比使用 JSONObject 更容易地获取它),你必须创建具有相同结构的类比你的json:

    public class principalClass {
         ArrayList<ResponseData> responseData;  
         ...
         //Getters, setters and constructors
    }
    
    public class ResponseData {
        public ArrayList<String> unitNames;
        public String subject;
        public UnitTopics unitTopics;
        ...
        //Getters, setters and constructors
    }
    
    public class UnitTopics {
        public ArrayList<Topics> first;
        public ArrayList<Topics> second;
        ...
        //Getters, setters and constructors
    }
    
    public class Topics {
        public String topicName;
        public String topicURL;
        ...
        //Getters, setters and constructors
    }
    

    类似的东西,然后你使用jackson将你的json传递给你的class principalClass:

    ObjectMapper obj= new ObjectMapper();
    PrincipalClass principal= obj.readValue(json, PrincipalClass.class);
    

    第二种可能性是将值转换为 JSONArray 和 JSONObject:

    JSONObject bodyJSON = new JSONObject(json);
    
    JSONArray responseData = bodyJSON.getJSONArray("responseData");
    
    JSONArray unitNames= responseData.getJSONArray(0);
    JSONObject subject= responseData.getJSONObject(1);
    ...
    

    如果你愿意,你可以循环一个 JSONArray:

    for (int i = 0; i < unitNames.length(); i++) {     
         String element = unitNames.getString(i);     
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用带有改造的 gson 转换器将您的 json 数据转换为 java 对象模型类

      implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
      

      或者您可以将 json 数据转换为模型类,如

      Gson gson = new Gson();
      String jsonInString = "{your json data}";
      ResponseModel response= gson.fromJson(jsonInString, ResponseModel.class);
      

      【讨论】:

        【解决方案3】:

        您是否尝试过将此 JSON 对象转换为 POJO。 我建议使用: This website

        它节省了大量的时间和精力。 这些将是您的模型类:

        package com.example.app;
        
        import java.io.Serializable;
        import java.util.List;
        import javax.annotation.Generated;
        import com.google.gson.annotations.Expose;
        import com.google.gson.annotations.SerializedName;
        
        
        public class ResponseDatum implements Serializable
        {
        
        @SerializedName("unitNames")
        @Expose
        private List<String> unitNames = null;
        @SerializedName("subject")
        @Expose
        private String subject;
        @SerializedName("unitTopics")
        @Expose
        private UnitTopics unitTopics;
        
        
        public ResponseDatum() {
        }
        
        
        public ResponseDatum(List<String> unitNames, String subject, UnitTopics unitTopics) {
        super();
        this.unitNames = unitNames;
        this.subject = subject;
        this.unitTopics = unitTopics;
        }
        
        public List<String> getUnitNames() {
        return unitNames;
        }
        
        public void setUnitNames(List<String> unitNames) {
        this.unitNames = unitNames;
        }
        
        public String getSubject() {
        return subject;
        }
        
        public void setSubject(String subject) {
        this.subject = subject;
        }
        
        public UnitTopics getUnitTopics() {
        return unitTopics;
        }
        
        public void setUnitTopics(UnitTopics unitTopics) {
        this.unitTopics = unitTopics;
        }
        
        }
        
        package com.example.app;
        
        import java.io.Serializable;
        import java.util.List;
        import javax.annotation.Generated;
        import com.google.gson.annotations.Expose;
        import com.google.gson.annotations.SerializedName;
        
        public class ResponseObject implements Serializable
        {
        
        @SerializedName("responseData")
        @Expose
        private List<ResponseDatum> responseData = null;
        
        
        public ResponseObject() {
        }
        
        
        public ResponseObject(List<ResponseDatum> responseData) {
        super();
        this.responseData = responseData;
        }
        
        public List<ResponseDatum> getResponseData() {
        return responseData;
        }
        
        public void setResponseData(List<ResponseDatum> responseData) {
        this.responseData = responseData;
        }
        
        }
        
        
        package com.example.app;
        
        import java.io.Serializable;
        import java.util.List;
        import javax.annotation.Generated;
        import com.google.gson.annotations.Expose;
        import com.google.gson.annotations.SerializedName;
        
        public class UnitTopics implements Serializable
        {
        
        @SerializedName("1")
        @Expose
        private List<com.example.app._1> _1 = null;
        @SerializedName("2")
        @Expose
        private List<com.example.app._2> _2 = null;
        
        
        
        public UnitTopics() {
        }
        
        
        public UnitTopics(List<com.example.app._1> _1, List<com.example.app._2> _2) {
        super();
        this._1 = _1;
        this._2 = _2;
        }
        
        public List<com.example.app._1> get1() {
        return _1;
        }
        
        public void set1(List<com.example.app._1> _1) {
        this._1 = _1;
        }
        
        public List<com.example.app._2> get2() {
        return _2;
        }
        
        public void set2(List<com.example.app._2> _2) {
        this._2 = _2;
        }
        
        }
        
        
        package com.example.app;
        
        import java.io.Serializable;
        import javax.annotation.Generated;
        import com.google.gson.annotations.Expose;
        import com.google.gson.annotations.SerializedName;
        
        public class _1 implements Serializable
        {
        
        @SerializedName("topicName")
        @Expose
        private String topicName;
        @SerializedName("topicURL")
        @Expose
        private String topicURL;
        
        
        
        
        public _1() {
        }
        
        
        public _1(String topicName, String topicURL) {
        super();
        this.topicName = topicName;
        this.topicURL = topicURL;
        }
        
        public String getTopicName() {
        return topicName;
        }
        
        public void setTopicName(String topicName) {
        this.topicName = topicName;
        }
        
        public String getTopicURL() {
        return topicURL;
        }
        
        public void setTopicURL(String topicURL) {
        this.topicURL = topicURL;
        }
        
        }
        
        
        package com.example.app;
        
        import java.io.Serializable;
        import javax.annotation.Generated;
        import com.google.gson.annotations.Expose;
        import com.google.gson.annotations.SerializedName;
        
        
        public class _2 implements Serializable
        {
        
        @SerializedName("topicName")
        @Expose
        private String topicName;
        @SerializedName("topicURL")
        @Expose
        private String topicURL;
        
        
        
        public _2() {
        }
        
        
        public _2(String topicName, String topicURL) {
        super();
        this.topicName = topicName;
        this.topicURL = topicURL;
        }
        
        public String getTopicName() {
        return topicName;
        }
        
        public void setTopicName(String topicName) {
        this.topicName = topicName;
        }
        
        public String getTopicURL() {
        return topicURL;
        }
        
        public void setTopicURL(String topicURL) {
        this.topicURL = topicURL;
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-13
          • 1970-01-01
          • 2018-12-20
          • 2014-08-26
          • 1970-01-01
          • 2020-02-09
          相关资源
          最近更新 更多