简体   繁体   中英

Parse JSON of two dimensional array in JAVA

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.

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM