简体   繁体   English

在JAVA中解析二维数组的JSON

[英]Parse JSON of two dimensional array in JAVA

Problem is, my JSON string looks like this: 问题是,我的JSON字符串如下所示:

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

I need to make a two dimensional array out of it in Java. 我需要用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. 你得到的JSON是一个数组,而不是一个对象。 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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