Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装POJO为空,但JSON有效_Android_Json_Pojo_Retrofit - Fatal编程技术网

Android 改装POJO为空,但JSON有效

Android 改装POJO为空,但JSON有效,android,json,pojo,retrofit,Android,Json,Pojo,Retrofit,这件事让我抓狂。JSON响应是有效的: { "MRData": { "xmlns": "http://ergast.com/mrd/1.4", "series": "f1", "url": "http://ergast.com/api/f1/current/2.json", "limit": "30", "offset": "0", "total": "1", "RaceTab

这件事让我抓狂。JSON响应是有效的:

{
    "MRData": {
        "xmlns": "http://ergast.com/mrd/1.4",
        "series": "f1",
        "url": "http://ergast.com/api/f1/current/2.json",
        "limit": "30",
        "offset": "0",
        "total": "1",
        "RaceTable": {
            "season": "2014",
            "round": "2",
            "Races": [
                {
                    "season": "2014",
                    "round": "2",
                    "url": "https://en.wikipedia.org/wiki/2014_Malaysian_Grand_Prix",
                    "raceName": "Malaysian Grand Prix",
                    "Circuit": {
                        "circuitId": "sepang",
                        "url": "http://en.wikipedia.org/wiki/Sepang_International_Circuit",
                        "circuitName": "Sepang International Circuit",
                        "Location": {
                            "lat": "2.76083",
                            "long": "101.738",
                            "locality": "Kuala Lumpur",
                            "country": "Malaysia"
                        }
                    },
                    "date": "2014-03-30",
                    "time": "08:00:00Z"
                }
            ]
        }
    }
}
响应的POJO:

public class ApiResponse {
    MRData mrdata;

    public class MRData {
        String xmlns;
        String series;
        String url;
        String limit;
        String offset;
        String total;
        RaceTable raceTable;
    }
}
apiResponse对象始终为空。任何人都可以在这里指出POJO对象的错误所在?
谢谢。

我在这里看到了几个潜在问题:

  • 您正在另一个类中定义一个类;我从未见过这样的事。您可能希望在两个不同的文件中分离

  • POJO上的变量名应该与JSON响应上的变量名完全匹配。例如:

  • 公共类ApiResponse{MRData MRData;}


    如果您希望POJO的变量与JSON返回的变量不同,您应该使用
    @SerlizedName

    我们在使用
    Gson
    反序列化JSON时遇到问题,发现区分大小写是罪魁祸首,因为我们可以在另一个类中使用类,这同样有效。@Emmanuel-您提出的第一点无效。我已经使用了相当长的一段时间,这种模式是非常普遍的。外壳是我的问题。!ThanksIt与其说是在表明自己的观点,不如说是在说“这可能会造成问题”。我从未见过这样做。很高兴你的问题解决了。