0

I have string like shown below:

{"data":"{\"methodName\":\"matchRequest\",\"arguments\":[{\"matchId\":2963,\"gamerName\":\"pro100_Ocean\",\"gamerId\":\"4c04d9f0-c1e7-410f-8ad8-a95922556bbd\",\"gamerFullName\":null,\"gamerPhotoUrl\":\"data\\\\user\\\\4c04d9f0-c1e7-410f-8ad8-a95922556bbd\\\\profile\\\\cropped3649162562321249118.jpg\",\"gamerRaiting\":1,\"gamerCardScore\":0,\"correctAnswerScore\":50,\"incorrectAnswerScore\":-50,\"isBot\":false,\"myCardScore\":0}],\"identifier\":\"00000000-0000-0000-0000-000000000000\"}"}

I take the text from the backend, and I need to take the data. How do I do it?

UPDATE

I want to use Gson, and I created Pojo class like below, but I needed to take the values as a string in the date key like json.(Perhaps you didn't understand me)

 data class RequestGameModel (
    @SerializedName("messageType")
    @Expose
    var messageType: Int? = null,

    @SerializedName("data")
    @Expose
    var data: String? = null)
Mukhit
  • 152
  • 2
  • 10
  • this is a Kotlin question and not a Java question. I vote to reopen. I added the Java tag on the other question since it was Java related back then and the language tag was missing in my view. – Willi Mentzel Feb 04 '19 at 23:25

3 Answers3

2

If you already know the expected structure of the data, I recommended using GSON. You can find a pretty good tutorial here, explaining how to deserialize a JSON string in the section Basics of JSON-Java Deserialization.

String userJson = "{'age':26,'email':'norman@futurestud.io','isDeveloper':true,'name':'Norman'}";  

Gson gson = new Gson();  
UserSimple userObject = gson.fromJson(userJson, UserSimple.class); 

For your case, first make sure that your JSON string is properly formatted. I think it should look like that:

{
    "data": {
        "methodName": "matchRequest",
        "arguments": [
            {
                "matchId": 2963,
                "gamerName": "pro100_Ocean",
                "gamerId": "4c04d9f0-c1e7-410f-8ad8-a95922556bbd",
                "gamerFullName": null,
                "gamerPhotoUrl": "data\\\\user\\\\4c04d9f0-c1e7-410f-8ad8-a95922556bbd\\\\profile\\\\cropped3649162562321249118.jpg",
                "gamerRaiting": 1,
                "gamerCardScore": 0,
                "correctAnswerScore": 50,
                "incorrectAnswerScore": -50,
                "isBot": false,
                "myCardScore": 0
            }
        ],
        "identifier": "00000000-0000-0000-0000-000000000000"
    }
}

So the data key in the root should be one of your models instead of a String. A possible model mapping is like following (I got rid of the @SerializedName and @Expose annotations to emphasize the structure):

data class RequestGameModel(
    val `data`: GameModel? = null
)

data class GameModel(
    val methodName: String? = null,
    val arguments: List<GameArguments>? = null,
    val identifier: String? = null
)

data class GameArguments(
    val matchId: Int = -1,
    val gamerName: String? = null,
    val gamerId: String? = null,
    val gamerFullName: String? = null,
    val gamerPhotoUrl: String? = null,
    val gamerRaiting: Int = 0,
    val gamerCardScore: Int = 0,
    val correctAnswerScore: Int = 0,
    val incorrectAnswerScore: Int = 0,
    val isBot: Boolean = false,
    val myCardScore: Int = 0
)

Note that I used val and not var, so make sure that you configured GSON to allow serialization of final fields.

Wang
  • 1,028
  • 7
  • 12
0

You can use JSONObject for that:

val jsonObj = JSONObject("""{"hello": "world"}""")

jsonObj.getString("hello") // world

Note: You will request the JSON most likely from a REST API, but if you may need to hardcode a JSON string or just want to play around with it, Kotlin's raw strings will become handy (starting and ending with a tripple quote), since you don't have to escape single quotes then (as you did using backslashes).

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
0

Once you have the JSON string, you can either use external libraries like GSON and jackson to parse it for you or you can simply use JSONObject and do it yourself.

JSONObject object = new JSONObject(jsonString)

Now you can easily fetch your data using various methods.

For example, to fetch a string value:

String value = object.getString("key")

to fetch a boolean value:

boolean value = object.getBoolean("key")

and so on. To know more check here

PS: Kindly search your queries before posting them. There are high chances that you'll find them answered already. You can find it already answered here.

Deepam Goel
  • 135
  • 1
  • 4
  • 8