Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用嵌套的方括号标记Java字符串?_Java_Regex_Json - Fatal编程技术网

如何用嵌套的方括号标记Java字符串?

如何用嵌套的方括号标记Java字符串?,java,regex,json,Java,Regex,Json,我有一个字符串,我想放入一个字符串数组列表中。字符串基本上是一个JSONObject,所以我可能只是使用了错误的方法 字符串的外观是: String all = "{"users": [ [{"login":"username1"},{"password":"test1"},{"index":"1"}], [{"login":"username2"},{"password":"test2"},{"index":"2"}] ]}"; 我想要的只是JSONObject值,因此我的模式为我提供了

我有一个字符串,我想放入一个字符串数组列表中。字符串基本上是一个JSONObject,所以我可能只是使用了错误的方法

字符串的外观是:

String all = "{"users":
[
 [{"login":"username1"},{"password":"test1"},{"index":"1"}],
 [{"login":"username2"},{"password":"test2"},{"index":"2"}]
]}";
我想要的只是JSONObject值,因此我的模式为我提供了以下字符串:

String part = "[
                [{"login":"username1"},{"password":"test1"},{"index":"1"}],
                [{"login":"username2"},{"password":"test2"},{"index":"2"}]
               ]";
这就是我想要的:

user[0] = "[{"login":"username1"},{"password":"test1"},{"index":"1"}]";
user[1] = "[{"login":"username2"},{"password":"test2"},{"index":"2"}]";
当我尝试将所有内容分组到内部[]之间时,它只返回外部[]中的所有内容

我试过:

String[] user = new String[20];
Pattern p = Pattern.compile("(\\[\\{.*\\}\\])");
Matcher m = p.matcher(part);
while(m.find()){
    user = m.group().split("\\],\\[");
}
这种方法去掉了,,[我用它作为分隔符。

类用户{
Class User {
  private String username;
  private String password;
}

Class Users{
  LinkedList<User> users;
}
私有字符串用户名; 私有字符串密码; } 类用户{ LinkedList用户; }

您可以使用任何可用的JSON封送器(如Jackson等)将字符串反序列化为
用户

),因此我采纳了评论部分的建议,确信使用JSON方法是可行的。我仍然想看看是否可以用正则表达式来实现

ArrayList<String> myList = new ArrayList<String>();
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();

obj = {"user":"[[{},{},{}],[{},{},{}]]";

// This gives me the outer JSONArray    
arr = obj.getJSONArray("user");

// This iterates through the outer JSONArray assigning each inner JSONArray
// to my ArrayList as strings.
for( int i = 0; i < arr.length(); i++){
    myList.put(arr.getJSONArray(i).toString());
}
ArrayList myList=new ArrayList();
JSONObject obj=新的JSONObject();
JSONArray arr=新的JSONArray();
obj={“用户”:“[{},{},{},{}],{},{},{}]];
//这给了我外部的JSONArray
arr=obj.getJSONArray(“用户”);
//这将遍历外部JSONArray,分配每个内部JSONArray
//作为字符串发送到我的ArrayList。
对于(int i=0;i
不要尝试使用regex创建一个,只需使用现有的JSON或YAML库来解析您的
字符串