0

I got this JSON response for the JSON Base (Let's say this is JSON_A):

{
  "result": [
    {
      "id": 1,
      "vendorTitle": "erlangga",
      "vendor_id": "178",
      "userstat": true
    }
  ],
  "stas": [],
  "code": 200,
}

And this JSON, I have to put this inside my base JSON, but I get confused because of the format. I don't know where should I put it inside my base JSON. Let's say this is JSON_B.

{
  "query": [
    [
      "vendorTitle",
      "consists",
      "erlangga"
    ],
    [
      "userstat",
      "true"
    ]
  ]
}

So, I use this to retrieve a list when the user tries to search for vendorName When I use Generate JSON to POJO Class in the Android studio plugin it only gives me this:

data class QueryResponse(
        val query: String
)

Is this the correct data class format for this kind of JSON? Where should I put my JSON_B to JSON_A. I'm kinda new to this kind of formatting, please kindly help me, Thank you, I really appreciate it a lot.

The response when using JSON_A :

nombu, britishpublication, erlangga, melayu, arabic

The response when using JSON_A and QueryResponse :

arabic, britishpublication, erlangga, melayu

My current DataClass looks like this :

data class JsonA(
    val code: Int,
    val result: List<String>,
    val userStat: Boolean
)

and JsonA result :

data class JsonA_Item(
        val id: Int,
        val vendorTitle: String,
        val vendor_id: String,
        val userstat: Boolean
)
6
  • what you can do is create 2 separate model class. call both API fetch data and at the end you can simply add one object to another pojo / model class.. Commented Jun 18, 2021 at 9:52
  • @AtifAbbAsi any example of that? Commented Jun 18, 2021 at 9:54
  • tell me one thing are you calling API successfully.? and mapping its response on model class or not.? Commented Jun 18, 2021 at 10:02
  • @AtifAbbAsi yes it is successfully works. basically like this : When user search for a value in a list-> the response will be like this { "query" : [["vendorTitle","contain","erlangga"],["userstat","true"]] } but when the user doesn't search for the value the response will look like this : { "query" : [["userstat","true"]] } Commented Jun 18, 2021 at 10:21
  • @AtifAbbAsi my current data class look like above, I've edited my description Commented Jun 18, 2021 at 10:25

2 Answers 2

1

Your JSON_A POJO should be like this:

data class JsonA(
    var code: Int?,
    var result: List<Result>?,
    var stas: List<Any>?
)

the Result POJO:

data class Result(
    var id: Int?,
    var userstat: Boolean?,
    var vendorTitle: String?,
    var vendor_id: String?
)

and the QueryResponse POJO:

data class QueryResponse(
    var query: List<List<String>>?
)
Sign up to request clarification or add additional context in comments.

3 Comments

What about the JSON_B?
Okay, thank you @MojoJojo but, I want to ask again, How do I call the QueryResponse to display this multidimensional list? , Do I need to call the JSON_A, instead of QueryResponse?. Because both of them will return 2 different lists, like the QueryResponse is the new update of JSON_A response
you can see the difference between that two JSON above, I've edited my description, thank you @MojoJojo
0

instead of creating multiple object classes simply merge your response in single object and use values according to your need.

  data class Response (
        val code: Int =-1,
        val result: List<String>=arrayListOf<String>(),
        val userStat: Boolean=false,
        val id: Int?=-1,
        val vendorTitle: String?="",
        val vendor_id: String?="",
        val userstat: Boolean =false
    )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.