How to parse a JSON string?


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.

The JSON.parse () method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned. JavaScript Demo: JSON.parse ()


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.

JSON.parse () takes a JSON string and transforms it into a JavaScript object. JSON.stringify () takes a JavaScript object and transforms it into a JSON string. Here’s an example: const myObj = { name: 'Skip', age: 2, favoriteFood: 'Steak' }; const myObjStr = JSON.stringify(myObj); console.log(myObjStr); // " {"name":"Skip","age":2,"favoriteFood":"Steak"}" console.log(JSON.parse(myObjStr)); // Object {name:"Skip",age:2,favoriteFood:"Steak"}


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).

I am trying to parse a JSON string in java to have the individual value printed separately. But while making the program run I get the following error- Exception in thread "main" java.lang.


JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays. An object is an unordered collection of zero or more name/value pairs. An array is an ordered sequence of zero or more values.


How to read JSON into .NET objects (deserialize) To deserialize from a string or a file, call the JsonSerializer.Deserialize method. The following example reads JSON from a string and creates an instance of the WeatherForecast class shown earlier for the serialization example:


Using JObject.Parse JObject class has parse method; it parses the JSON string and converts it into a Key-value dictionary object. In the following example, I have used “JObject.Parse” method and retrieved data using key. string jsonData = @" {