3

Problem is, my JSON string looks like this:

jsonString =  [["1","100"],["2","200"],["3","300"]]

I need to make a two dimensional array out of it in Java. If I write

JSONObject jObs = new JSONObject(jsonString);

I get the following error:

A JSONObject text must begin with '{' at character 1 of [["1 ...

How can I parse a two dimensional array out of this string? Thanks in advance.

1 Answer 1

7

The JSON you've got is for an array, not an object. You probably want

JSONArray array = new JSONArray(jsonString);

Full sample code:

import org.json.*;

public class Test {
    public static void main(String[] args) {
        String json = "[[\"1\",\"100\"],[\"2\",\"200\"],[\"3\",\"300\"]]";
        JSONArray array = new JSONArray(json);
        JSONArray first = array.getJSONArray(0);
        System.out.println(first.getString(1)); // Prints 100
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+vote After nearly one hour surfing on the net i find what i need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.