Arpit Patel
Arpit Patel

Reputation: 8057

Unable to generate POJO(model) class using Gson format

I want to parse JSON using Retrofit 2.1.0 library so i used Gson format plugin for creating POJO (model) class but I am not able to create it because of this Gson format exception error --

 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 ; '

And my JSON is also valid I am validating using this site http://jsonformatter.org/

Here is my 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/[email protected]",
        "bIsDefault": "1"
      },
      {
        "iImageData": "http:///images/tour-images/[email protected]",
        "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"
    }
  }
}

I know there is http://www.jsonschema2pojo.org/ to generate model class but it create separate classes not nested class so it's hard to manage so many classes.

QUE 1: How can i generate model class using json?

QUE 2: Is there any way i can get string response using retrofit 2.1.0 i tried RequestBody but not work.

Is there any better solution for that??

Upvotes: 1

Views: 916

Answers (2)

iagreen
iagreen

Reputation: 32026

1) I am not too familiar with the Gson formatter plugin, but it looks like it is generating an invalid type name "0Bean". It looks like you can edit the names and types of what it generates. Otherwise, using the site you mentioned or hand coding the POJOs are options. Also, if you able to rename elements in your JSON, don't name fields with numbers like "0".

2) You can get a string response by using the scalars converter --

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();

and in your interface --

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

You will need to add -- compile 'com.squareup.retrofit2:converter-scalars:2.1.0' to your gradle file.

Upvotes: 0

Lovekesh
Lovekesh

Reputation: 125

enter image description here

enter image description here

This is the sample of your json object.Please check it ,try with that ways.

Upvotes: 1

Related Questions