Siddharth Sinha Siddharth Sinha - 1 year ago 90
JSON Question

How to convert a string to JSONArray

I am hitting an api and getting a string response. The response is something like this

"["VU","Vanuatu",["Pacific/Efate","(GMT+11:00) Efate"],"VN","Vietnam",["Asia/Saigon","(GMT+07:00) Hanoi"]]"

And i want to convert this string into a json array of below type

[{"id":"VN","name":"Vanuatu","timezones":[{"id":Pacific/Efate,"name":"(GMT+11:00) Efate}]},"id":"VN","name":"Vietnam",[{"id":"Asia/Saigon","name":"(GMT+07:00) Hanoi"}]]

Can someone help

rD. rD.
Answer Source

Looking at your String response, I've created a regular expression that will create four groups out of your response.


Assuming that your output would come always in groups of four (i.e., id, name and timezones_id, timezones_name), this regular expression, would extract 4 groups out of the input string that you've provided:




Match 1
    Full match  1-56    `"VU", "Vanuatu", ["Pacific/Efate", "(GMT+11:00) Efate"]`
        Group 1.    2-4     `VU`
        Group 2.    8-15    `Vanuatu`
        Group 3.    20-33   `Pacific/Efate`
        Group 4.    37-54   `(GMT+11:00) Efate`
Match 2
    Full match  58-111  `"VN", "Vietnam", ["Asia/Saigon", "(GMT+07:00) Hanoi"]`
        Group 1.    59-61   `VN`
        Group 2.    65-72   `Vietnam`
        Group 3.    77-88   `Asia/Saigon`
        Group 4.    92-109  `(GMT+07:00) Hanoi`


Now once you've extracted these 4 groups, You can simply add appropriately in ArrayList and List and create JSONArray out of those lists.

The following program is self-explanatory with the inputs and outputs.


["VU","Vanuatu",["Pacific/Efate","(GMT+11:00) Efate"],"VN","Vietnam",["Asia/Saigon","(GMT+07:00) Hanoi"]]


[{"timezones":{"name":"(GMT+11:00) Efate","id":"Pacific/Efate"},"name":"Vanuatu","id":"VU"},{"timezones":{"name":"(GMT+07:00) Hanoi","id":"Asia/Saigon"},"name":"Vietnam","id":"VN"}]

Formatted Output


        "id" : "VU",
        "name" : "Vanuatu",
        "timezones" : {
            "name" : "(GMT+11:00) Efate",
            "id" : "Pacific/Efate"
    }, {
        "id" : "VN",
        "name" : "Vietnam",
        "timezones" : {
            "name" : "(GMT+07:00) Hanoi",
            "id" : "Asia/Saigon"


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;

public class Test
    public static void main(String[] args) throws IOException, JSONException {
        String serverResponse = "[\"VU\", \"Vanuatu\", [\"Pacific/Efate\", \"(GMT+11:00) Efate\"], \"VN\", \"Vietnam\", [\"Asia/Saigon\", \"(GMT+07:00) Hanoi\"]]";
        Map<String, Object> prop, innerProp;
        List<Object> arr = new ArrayList<>(), obj;

        String pattern = "\"([^\"]*)\",\\s*\"([^\"]*)\",\\s*\\[\"([^\"]*)\",\\s*\"([^\"]*)\"\\]";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(serverResponse);

        while (m.find()) {
            prop = new HashMap<>();

            innerProp = new HashMap<>();

            prop.put("timezones", innerProp);

        JSONArray jsonArray = new JSONArray(arr);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download