March 2019
Intermediate to advanced
242 pages
6h 21m
English
Suppose that you have the following JSON data to be stored as a Java string:
{"plastic": {
"id": "98751",
"singleuse": {
"item": [
{"value": "Water Bottle", "replaceWith": "Steel Bottle()"},
{"value": "Straw", "replaceWith": "Ban Straws"},
{"value": "Spoon", "replaceWith": "Steel Spoon"}
]
}
}}
The following code shows how you would perform this action with the traditional String class, escaping the double quotes within the data by using \ and adding a newline escape sequence:
String data =
"{\"plastic\": { \n" +
"\"id\": \"98751\", \n" +
"\"singleuse\": { \n" +
"\"item\": [ \n" +
"{\"value\": \"Water Bottle\", \"replaceWith\": \"Steel Bottle()\"}, \n" + "{\"value\": \"Straw\", \"replaceWith\": \"Ban Straws\"}, \n" + "{\"value\": ...Read now
Unlock full access