javajson

How to pass dynamic value to a JSON String


I have constructed a JSON String this way, but cannot pass dynamic values to it

String input = "{\r\n" + 
                    "    \"Level\": 0,\r\n" + 
                    "    \"Name\": \"String\",\r\n" + 
                    "    \"msgName\": \"String\",\r\n" + 
                    "    \"ActualMessage\": \"String\",\r\n" + 
                    "    \"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" + 
                    "}" ;

String message = "this is value  want to pass to the ActualMessage attribute " ;

I need to pass dynamic value to the ActaulMessage atribute 

Please tell me how.

I have tried number of trial and errors but couldn't succeed.


Solution

  • Use string concatenation.

    String message = "this is value  want to pass to the ActualMessage attribute " ;
    String input = "{\r\n" + 
                   "\"Level\": 0,\r\n" + 
                   "\"Name\": \"String\",\r\n" + 
                   "\"msgName\": \"String\",\r\n" + 
                   "\"ActualMessage\": \"" + message + "\",\r\n" + 
                   "\"TimeStamp\": \"/Date(-62135596800000-0000)/\"\r\n" + 
                   "}" ;