Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.


trying to create JSON string using 2D String array

  • Alan Shiers
    Alan Shiers

    Hi there,

    I'm trying to create a JSON string using a 2D String array. Each String must have the format: "field_name":"field_value"

    I want the output to be like:
    ["LASTNAME":"MacDonald", "FIRSTNAME":"Terry", "EMAIL_ADDRESS":""],["LASTNAME":"Steele","FIRSTNAME":"Paul","EMAIL_ADDRESS":""]

    I've tried the following, which unfortunately doesn't output the strings properly:
    String[][] array = new String[][];
    array[0][0] = "\"LASTNAME\":\"MacDonald\"";
    array[0][1] = "\"FIRSTNAME\":\"Terry\"";
    array[0][2] = "\"EMAIL_ADDRESS\":\"\"";
    JSONObject jsonArray = JSONArray.fromObject(array);
    System.out.println( jsonObject );

    This results in:
    ["\"LASTNAME\":\"MacDonald\"", "\"FIRSTNAME\":\"Terry\"", "\"EMAIL_ADDRESS\":\"\""],

    Escaping the quote symbol obviously isn't the answer here. What do I need to do to get the format I'm looking for? Please advise.