无法使用Gson格式

时间:2016-12-11 08:57:08

标签: android retrofit2

我想使用Retrofit 2.1.0库解析JSON,所以我使用Gson格式插件来创建POJO(模型)类,但由于此Gson格式异常错误,我无法创建它 -

 com.intellij.util.IncorrectOperationException: Incorrect field '/** 
           * 0 : {"lat":"23.0124","long":"72.5228","question":"caluculate 1+2","hint":"1,2,3","questionAudioUrl":"http://audio/1480919241.mp3"}
           * type : type1
           */ 
           @com.google.gson.annotations.SerializedName("0")private  0Bean value0 ; '

我的JSON也有效我正在验证使用此网站http://jsonformatter.org/

这是我的 json

    {
  "response": {
    "result": true,
    "errorCode": 0,
    "errorDescription": "Success",
    "tourData": {
      "TourID": "5",
      "sTourName": "titanium city center ahmedabad",
      "sShortDescription": "ffjk fdjf fdjf fd f jkdf fjdf fdf f fhfjd fj jfjfjdhfhfhfdf f fjhf",
      "sLongDescription": "dsd dsd ds dsd dsd d sdsd ds dsd sds dd",
      "sCity": "ahmedabad",
      "sLatitude": "23.0124",
      "sLongitude": "72.5228",
      "sGoalLat": "-2.09",
      "sGoalLong": "12.5228",
      "sVersion": "1",
      "dDate": "2016-11-29 00:41:32",
      "status": "APPROVED",
      "iTourImagesID": "18",
      "iTourID": "5",
      "iImageData": "http://myurl/images/tour-images/1480405309background_menu.jpeg",
      "bIsDefault": "1",
      "dDateTime": "2016-11-29 13:11:49"
    },
    "tourCountData": {
      "total_image": "4",
      "total_coin": "12",
      "total_diamond": "1",
      "total_clues": "3",
      "total_checkpoint": "1"
    },
    "TourAllImageData": [
      {
        "iImageData": "http:///images/tour-images/1480405309background_menu.jpeg",
        "bIsDefault": "1"
      },
      {
        "iImageData": "http://images/tour-images/1480405310background_menu@2x.jpeg",
        "bIsDefault": "1"
      },
      {
        "iImageData": "http:///images/tour-images/1480405312background_menu-568@2x.jpeg",
        "bIsDefault": "1"
      },
      {
        "iImageData": "http://images/tour-images/1480405313background_menu@2x~ipad.jpeg",
        "bIsDefault": "1"
      }
    ],
    "img_result": true,
    "TourAllUserData": [],
    "u_result": false,
    "TourcoinsData": [
      {
        "sLatitude": "19.422655",
        "sLongitude": "-99.161297",
        "iTourPlaceID": "10"
      },
      {
        "sLatitude": "23.0135",
        "sLongitude": "72.5228",
        "iTourPlaceID": "12"
      },
      {
        "sLatitude": "23.0150",
        "sLongitude": "72.5296",
        "iTourPlaceID": "13"
      },
      {
        "sLatitude": "23.0150",
        "sLongitude": "72.5245",
        "iTourPlaceID": "14"
      },
      {
        "sLatitude": "23.0160",
        "sLongitude": "72.5310",
        "iTourPlaceID": "15"
      },
      {
        "sLatitude": "23.0115",
        "sLongitude": "72.5228",
        "iTourPlaceID": "16"
      },
      {
        "sLatitude": "23.0135",
        "sLongitude": "72.5296",
        "iTourPlaceID": "17"
      },
      {
        "sLatitude": "23.0115",
        "sLongitude": "72.5200",
        "iTourPlaceID": "18"
      },
      {
        "sLatitude": "23.0135",
        "sLongitude": "72.5270",
        "iTourPlaceID": "19"
      },
      {
        "sLatitude": "23.0135",
        "sLongitude": "72.5260",
        "iTourPlaceID": "20"
      },
      {
        "sLatitude": "23.0160",
        "sLongitude": "72.5296",
        "iTourPlaceID": "21"
      },
      {
        "sLatitude": "23.0170",
        "sLongitude": "72.5310",
        "iTourPlaceID": "22"
      }
    ],
    "co_result": true,
    "TourDiamondData": [
      {
        "sLatitude": "23.011487",
        "sLongitude": "72.525011",
        "iTourPlaceID": "24",
        "sPlaceImage": "http:/images/places/1481007650-place.png",
        "sPlaceDescription": "dd d dh hffh idufhfhf hf hfhd fhf hdfhufh ffdufh fhdfu dfhd fhd fhdfhdufduf hfhdfhd fhdfhdf dfhdufd ffdufh fhdufd ffhduf ufufh"
      }
    ],
    "di_result": true,
    "TourCheckpointData": [
      {
        "sLatitude": "23.0225",
        "sLongitude": "72.5714",
        "iTourPlaceID": "23"
      }
    ],
    "chk_result": true,
    "TourClueData": [
      {
        "sClues": "clue1"
      },
      {
        "sClues": "clue2"
      },
      {
        "sClues": "clue3"
      }
    ],
    "clu_result": true,
    "TourChallenge1Data": {
      "0": {
        "lat": "23.0124",
        "long": "72.5228",
        "question": "caluculate 1+2",
        "hint": "1,2,3",
        "questionAudioUrl": "http://audio/1480919241.mp3"
      },
      "type": "type1"
    },
    "TourChallenge2Data": {
      "0": {
        "lat": "23.0143",
        "long": "72.5296",
        "question": "type2",
        "hint": "1,,,2",
        "timer": "01:01:00",
        "questionAudioUrl": "http://audio/1480919413.mp3"
      },
      "type": "Type2"
    },
    "TourChallenge3Data": {
      "0": {
        "lat": "23.011641",
        "long": "72.523917",
        "puzzleImageUrl": "http:/images/puzzles/1480919458-puzzle.png",
        "puzzleDescription": "set image",
        "puzzleAudioUrl": "http:/audio/1480919458.mp3"
      },
      "type": "Type3"
    },
    "TourChallenge4Data": {
      "0": {
        "lat": "23.0124",
        "long": "72.5210",
        "keyLat": "23.0135",
        "keyLong": "72.5228"
      },
      "type": "Type4"
    }
  }
}

我知道有http://www.jsonschema2pojo.org/来生成模型类,但是它创建了单独的类而不是嵌套类,因此很难管理这么多类。

QUE 1:如何使用json生成模型类?

QUE 2:有什么方法可以使用改装2.1.0获得字符串响应我试过RequestBody但不行。

有没有更好的解决方案??

2 个答案:

答案 0 :(得分:1)

enter image description here

enter image description here

这是你的json对象的样本。请检查它,尝试这种方式。

答案 1 :(得分:0)

1)我不太熟悉Gson格式化程序插件,但看起来它生成了一个无效的类型名称“0Bean”。看起来您可以编辑它生成的名称和类型。否则,使用您提到的站点或手工编写POJO是可选的。此外,如果您能够重命名JSON中的元素,请不要将数字命名为“0”。

2)您可以使用标量转换器获取字符串响应 -

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(/* your url */)
    .addConverterFactory(ScalarsConverterFactory.create())
    .addConverterFactory(GsonConverterFactory.create()) /* if you also still need GSON, if not, you can leave this off */
    .build();

并在您的界面中

@GET("/mypath")
Call<String> getMyStuff();

您需要将 - compile 'com.squareup.retrofit2:converter-scalars:2.1.0'添加到您的gradle文件中。