【发布时间】:2017-08-31 12:12:03
【问题描述】:
我正在从 url 连接中获取一段 JSON 文本,并将其保存到当前的字符串中:
...//setting up url and connection
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String str = in.readLine();
当我打印str时,我正确找到了数据{"build":{"version_component":"1.0.111"}}
现在我想从 str 中提取 111,但我遇到了一些麻烦。
我试过了
String afterLastDot = inputLine.substring(inputLine.lastIndexOf(".") + 1);
但我最终得到111"}}
我需要一个通用的解决方案,以便如果我有 String str = {"build":{"version_component":"1.0.111111111"}};,该解决方案仍然有效并提取 111111111(即,我不想硬编码提取小数点后的最后三位)
【问题讨论】:
-
嗯。那么你在哪里使用正则表达式?
-
你可以做 JSON
-
json.getBuild.getVersion_component
-
您发布的代码没有意义。你的意思是整个事情都在一个字符串中?即
"String str = {\"build\":{\"version_component\":\"1.0.111\"}};"或者你的意思是String str = "{\"build\":{\"version_component\":\"1.0.111\"}};" -
字符串值必须以"开头并以"结尾