简体   繁体   中英

Parsing nested json array using Gson

I have a json response from the server as follows:

 [
[
{
"room":{
"font_color":"#432f2f",
"video_brief":"",
"id":70,
"background_image":"/media/avatars/2018-07-31t1053231090620000-CISLogo.png",
"room_name":"asdasdad",
"logo":"/media/avatars/2018-07-31t1053060036050000-escape-room.jpg",
"widget_header_color":"#6f3c3c",
"display_timer_milliseconds":true,
"hint":false,
"hide_timer":true,
"default_time_limit":60,
"admin_pin":"123a",
"final_code":"",
"background_color":"#3a5da7",
"hint_tap_exit":true,
"start_timer_after_video_brief":true,
"hints_full_screen":true
},
"ending":{
"success_ending_type":"video ending",
"success_video":"/media/avatars/2018-07-31t1049359528150000-CISLogo.png",
"success_timer_size":10,
"success_hide_timer":false,
"success_screen_footer":"Lorem Ipsum is simply dummy text of the printing and typesetting",
"failure_screen_header":"Lorem Ipsum is simply dummy text of the printing and typesetting",
"game_end_screen_background_color":"#0a4913",
"end_game_failure_background":"/media/avatars/2018-07-31t1049359524170000-Screenshot_from_2018-07-19_12-30-48.png",
"fail_timer_size":10,
"timer_font_size":10,
"game_end_screen_font_color":"#090808",
"fail_font_size":"",
"fail_video":"",
"fail_hide_timer":false,
"failure_screen_footer":"Lorem Ipsum is simply dummy text of the printing and typesetting",
"fail_ending_type":"text ending",
"end_game_success_background":"/media/avatars/2018-07-31t1049359517220000-escape-room.jpg",
"success_screen_header":"Lorem Ipsum is simply dummy text of the printing and typesetting"
},
"puzzle":[
{
"puzzle_name":"sdfsfsd",
"dashboard_icon":"/media/avatars/2018-07-31t1053531164280000-escape-room.jpg",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"/media/avatars/2018-08-01t0734435036700000-Screenshot_from_2018-07-19_12-30-41.png",
"clue_checkbox":true,
"score_penatly":2,
"clue_file_uploads":"/media/avatars/2018-07-26t1611117099590000-CISLogo.png",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:34:43.505Z",
"clue_uploads_options":"Text",
"clue_textarea":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
},
{
"clue_type":"Medium Clue",
"clue_icon":"/media/avatars/2018-07-26t1617207596990000-S.png",
"clue_checkbox":false,
"score_penatly":2,
"clue_file_uploads":"/media/avatars/2018-07-26t1617485334340000-Last_year_close_project",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"Image",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":123,
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":20,
"enabled_puzzle_clue":false
},
{
"puzzle_name":"dfgdfgfdg",
"dashboard_icon":"",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"/media/avatars/2018-07-31t1046590997780000-CISLogo.png",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"Text",
"clue_textarea":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"
},
{
"clue_type":"Medium Clue",
"clue_icon":"/media/avatars/2018-07-31t1047416329850000-S.png",
"clue_checkbox":true,
"score_penatly":"",
"clue_file_uploads":"/media/avatars/2018-07-31t1047513793500000-escape-room.jpg",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"Image",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":true,
"score_penatly":12,
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":21,
"enabled_puzzle_clue":false
},
{
"puzzle_name":"null puzzle for testing",
"dashboard_icon":"",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"",
"clue_textarea":""
},
{
"clue_type":"Medium Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T07:26:54.005Z",
"clue_updated":"2018-08-01T07:26:54.104Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":22,
"enabled_puzzle_clue":false
},
{
"puzzle_name":"video puzzle",
"dashboard_icon":"",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"/media/avatars/2018-08-01t1355436442120000-terminal_demo.png",
"clue_checkbox":true,
"score_penatly":21,
"clue_file_uploads":"/media/avatars/2018-08-01t1354499320560000-video-1532193678.mp4",
"clue_created":"2018-08-01T13:53:45.849Z",
"clue_updated":"2018-08-01T13:55:43.644Z",
"clue_uploads_options":"Video",
"clue_textarea":""
},
{
"clue_type":"Medium Clue",
"clue_icon":"",
"clue_checkbox":true,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T13:53:45.850Z",
"clue_updated":"2018-08-01T13:53:45.850Z",
"clue_uploads_options":"Text",
"clue_textarea":"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T13:53:45.850Z",
"clue_updated":"2018-08-01T13:53:45.850Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":23,
"enabled_puzzle_clue":false
},
{
"puzzle_name":"Audio puzzle clue",
"dashboard_icon":"",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"",
"clue_checkbox":true,
"score_penatly":"",
"clue_file_uploads":"/media/avatars/2018-08-01t1402533781470000-2018-03-07t1134009670350000-2017-11-23t1448495014360000-wakeupsid01www.songs.pk.mp3",
"clue_created":"2018-08-01T14:00:29.256Z",
"clue_updated":"2018-08-01T14:02:53.380Z",
"clue_uploads_options":"Image",
"clue_textarea":""
},
{
"clue_type":"Medium Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T14:00:29.256Z",
"clue_updated":"2018-08-01T14:00:29.256Z",
"clue_uploads_options":"",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-01T14:00:29.256Z",
"clue_updated":"2018-08-01T14:00:29.256Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":24,
"enabled_puzzle_clue":false
},
{
"puzzle_name":"test 2",
"dashboard_icon":"/media/avatars/2018-08-02t0904085677080000-2018-03-08t1307539658100000-2017-11-23t1247143761860000-profile.jpg",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"",
"clue_checkbox":true,
"score_penatly":23,
"clue_file_uploads":"/media/avatars/2018-08-02t0904174183280000-2018-03-19t0733267326320000-2017-11-23t1247143761860000-profile.jpg",
"clue_created":"2018-08-02T09:03:01.337Z",
"clue_updated":"2018-08-02T09:04:17.418Z",
"clue_uploads_options":"Image",
"clue_textarea":""
},
{
"clue_type":"Medium Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-02T09:03:01.337Z",
"clue_updated":"2018-08-02T09:03:01.337Z",
"clue_uploads_options":"",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-02T09:03:01.337Z",
"clue_updated":"2018-08-02T09:03:01.337Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":25,
"enabled_puzzle_clue":false
}
],
"scoring":{
"end_game_at_zero":true,
"always_reduce":true,
"score_title":"Score",
"enable_scoring":false,
"starting_score":1,
"penalty_per_minute":200,
"is_penalize":false
}
}
],
[
{
"room":{
"font_color":"",
"video_brief":"",
"id":71,
"background_image":"",
"room_name":"testing 2",
"logo":"",
"widget_header_color":"",
"display_timer_milliseconds":false,
"hint":false,
"hide_timer":false,
"default_time_limit":60,
"admin_pin":"1234",
"final_code":"",
"background_color":"",
"hint_tap_exit":true,
"start_timer_after_video_brief":true,
"hints_full_screen":true
},
"ending":{
"success_ending_type":"Video Ending",
"success_video":"",
"success_timer_size":"",
"success_hide_timer":false,
"success_screen_footer":"",
"failure_screen_header":"",
"game_end_screen_background_color":"",
"end_game_failure_background":"",
"fail_timer_size":"",
"timer_font_size":"",
"game_end_screen_font_color":"",
"fail_font_size":"",
"fail_video":"",
"fail_hide_timer":false,
"failure_screen_footer":"",
"fail_ending_type":"Video Ending",
"end_game_success_background":"",
"success_screen_header":""
},
"puzzle":[
{
"puzzle_name":"new testing",
"dashboard_icon":"/media/avatars/2018-08-02t0916137895540000-2018-04-04t1204451779930000-profile.jpg",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"/media/avatars/2018-08-02t0916440614750000-2017-11-23t1247143761860000-profile.jpg",
"clue_checkbox":true,
"score_penatly":"",
"clue_file_uploads":"/media/avatars/2018-08-02t0917196773170000-2018-03-07t1134009670350000-2017-11-23t1448495014360000-wakeupsid01www.songs.pk.mp3",
"clue_created":"2018-08-02T09:15:57.206Z",
"clue_updated":"2018-08-02T09:17:19.682Z",
"clue_uploads_options":"Text",
"clue_textarea":"kasdk;asdkl;asdaskkasdl;kd;lkd;asldl;sa"
},
{
"clue_type":"Medium Clue",
"clue_icon":"/media/avatars/2018-08-02t0925267752970000-2018-06-02t0837526299890000-2017-11-23t1308299400930000-update_attendance.png",
"clue_checkbox":true,
"score_penatly":12,
"clue_file_uploads":"/media/avatars/2018-08-02t0926065833520000-2018-05-11t1216139697320000-2017-11-23t1448495014360000-wakeupsid01www.songs.pk.mp3",
"clue_created":"2018-08-02T09:15:57.206Z",
"clue_updated":"2018-08-02T09:26:06.585Z",
"clue_uploads_options":"Audio",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-02T09:15:57.206Z",
"clue_updated":"2018-08-02T09:15:57.206Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":26,
"enabled_puzzle_clue":true
},
{
"puzzle_name":"new testing 2",
"dashboard_icon":"",
"puzzle_clue":[
{
"clue_type":"Easy Clue",
"clue_icon":"/media/avatars/2018-08-02t0926554677370000-2018-03-19t0814015731280000-update_attendance.png",
"clue_checkbox":true,
"score_penatly":"",
"clue_file_uploads":"/media/avatars/2018-08-02t0926585264530000-2017-11-23t1247143761860000-profile.jpg",
"clue_created":"2018-08-02T09:26:26.135Z",
"clue_updated":"2018-08-02T09:26:58.526Z",
"clue_uploads_options":"Image",
"clue_textarea":""
},
{
"clue_type":"Medium Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-02T09:26:26.135Z",
"clue_updated":"2018-08-02T09:26:26.135Z",
"clue_uploads_options":"",
"clue_textarea":""
},
{
"clue_type":"Hard Clue",
"clue_icon":"",
"clue_checkbox":false,
"score_penatly":"",
"clue_file_uploads":"",
"clue_created":"2018-08-02T09:26:26.135Z",
"clue_updated":"2018-08-02T09:26:26.135Z",
"clue_uploads_options":"",
"clue_textarea":""
}
],
"puzzle_id":27,
"enabled_puzzle_clue":false
}
],
"scoring":{
"end_game_at_zero":false,
"always_reduce":false,
"score_title":"Score",
"enable_scoring":false,
"starting_score":"",
"penalty_per_minute":"",
"is_penalize":false
}
}
]
]

and the method for parsing this json array is :

 val listNew:ArrayList<ArrayList<BaseRoomBean>> = Gson().fromJson(value.body()
?.asJsonArray, object : TypeToken<ArrayList<ArrayList<BaseRoomBean>>>() {}.type)

but getting following error:

Type inferece failed: Not enough information to infer parameter T in 
operator fun <T> JSONArray.iterator () L Iterator<T>

Tried alot of solution but failed.

Here is the Model:

public class BaseRoomBean extends RealmObject implements Parcelable {

    @SerializedName("room")
    @Expose
    private RoomModel room;
    @SerializedName("ending")
    @Expose
    private EndingModel ending;
    @SerializedName("puzzle")
    @Expose
    private RealmList<PuzzleModel> puzzle = new RealmList<>();
    @SerializedName("scoring")
    @Expose
    private ScoringModel scoring;


    public BaseRoomBean() {
    }


    protected BaseRoomBean(Parcel in) {
        room = (RoomModel) in.readParcelable(RoomModel.class.getClassLoader());
        ending = (EndingModel) in.readParcelable(EndingModel.class.getClassLoader());
        scoring = (ScoringModel) in.readParcelable(ScoringModel.class.getClassLoader());
        in.readList(puzzle,List.class.getClassLoader());
    }

    public static final Creator<BaseRoomBean> CREATOR = new Creator<BaseRoomBean>() {
        @Override
        public BaseRoomBean createFromParcel(Parcel in) {
            return new BaseRoomBean(in);
        }

        @Override
        public BaseRoomBean[] newArray(int size) {
            return new BaseRoomBean[size];
        }
    };

    public RoomModel getRoom() {
        return room;
    }

    public void setRoom(RoomModel room) {
        this.room = room;
    }

    public EndingModel getEnding() {
        return ending;
    }

    public void setEnding(EndingModel ending) {
        this.ending = ending;
    }

    public RealmList<PuzzleModel> getPuzzle() {
        return puzzle;
    }

    public void setPuzzle(RealmList<PuzzleModel> puzzle) {
        this.puzzle = puzzle;
    }

    public ScoringModel getScoring() {
        return scoring;
    }

    public void setScoring(ScoringModel scoring) {
        this.scoring = scoring;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeParcelable(room, i);
        parcel.writeParcelable(ending, i);
        parcel.writeParcelable(scoring, i);
        parcel.writeList(puzzle);
    }
}

Kotlin is a strongly typed language that supports type inference or deduction. This is the mechanism employed by the compiler to find out types from context.

Validate JSON at https://jsonlint.com/ You will get Parse error

You are getting this error: Type inferece failed as your body and parsing type is not matching or syntax error

you must have strong knowledge of json array and json object... my suggestion is to create model class using online json schema. your json response's parent is array of array... so just pass model in format like, root - Json Array parent - Json Array child - Json Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM