java:如何将此字符串转换为ArrayList? [英] java: how to convert this string to ArrayList?

查看:250
本文介绍了java:如何将此字符串转换为ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String text = '[["item1","item2","item3"], ["some", "item"], ["far", "out", "string"]]';

我想迭代每个单独的ArrayList。我不知道如何将该字符串转换为适当的ArrayList对象。

I would like to iterate over each individual ArrayList. I don't know how to turn that string into appropriate ArrayList object.

推荐答案

此语法看起来像JSON的一个子集,并且我猜测客户端实际上将其编码为JSON。假设这是真的,最简单的方法是使用现成的JSON解析器,以及一些简单的Java代码将结果对象转换为代码所需的形式。

This syntax looks like a subset of JSON, and I would guess that the client side is actually encoding it as JSON. Assuming that is true, the simplest approach will be to use an off-the-shelf JSON parser, and some simple Java code to convert the resulting objects into the form that your code requires.

当然,您可以手动实现自己的解析器,但它可能不值得付出努力,特别是如果您必须处理字符串转义,空格中可能的可变性等等。不要忘记,如果您实现自己的解析器,需要执行单元测试以确保它适用于所有预期的有效输入,以及无效输入。 (测试无效输入的情况很重要,因为如果某些黑客发送包含错误输入的请求,您不希望服务器崩溃。)

Sure, you could implement your own parser by hand, but it is probably not worth the effort, especially if you have to deal with string escaping, possible variability in whitespaces and so on. Don't forget that if you implement your own parser, you NEED TO IMPLEMENT UNIT TESTS to make sure that it works across the full range of expected valid input, and for invalid input as well. (Testing the cases of invalid input is important because you don't want your server to fall over if some hacker sends requests containing bad input.)

在您继续之前,您确实需要确认客户端发送给您的确切语法。仅仅看一个例子就不会回答这个问题。您需要一个指定语法的文档,或者您需要查看客户端/应用程序源代码。

Before you go any further, you really need to confirm the exact syntax that the client is sending you. Just looking at an example is not going to answer that. You either need a document specifying what the syntax is, or you need to look at the client / application source code.

这篇关于java:如何将此字符串转换为ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆