如何解析 java 中的多个 json 值
How to parse multipe json value in java
我的 StringBuilder 变量中的值低于 json,我想解析所有 id 键值并将其再次存储在 StringBuilder 中。
{"status":"success","id":"1"}
{"status":"success","id":"2"}
{"status":"success","id":"3"}
{"status":"success","id":"4"}
{"status":"success","id":"5"}
{"status":"success","id":"6"}
预期输出:
1个
2个
3个
4个
5个
6
如何在 java 中解析这些值?
我尝试了以下选项,但没有帮助:
StringBuilder str = new StringBuilder();
str.append(jsonStringValue);
JSONObject jObj = new JSONObject(str);
jObj.getString("id");
使用 JSONTokener
JSONTokener t = new JSONTokener(str.toString());
while (t.more()) {
JSONObject o2 = (JSONObject) t.nextValue();
System.out.println(o2.getString("id"));
}
但我收到以下错误消息:
org.json.JSONException:字符 128 处缺少值
如果您正在使用 org.json,您可以使用 JSONTokener。
下面的例子展示了它是如何工作的。
public static void main(String args[]) throws JSONException {
String str1 = "{\"strValue\":\"string\"}\n{\"intValue\":1}";
JSONTokener t = new JSONTokener(str1);
JSONObject o1 = (JSONObject) t.nextValue();
JSONObject o2 = (JSONObject) t.nextValue();
System.out.println(o1.getString("strValue"));
System.out.println(o2.getLong("intValue"));
System.out.println(t.more()); // Check if there's more token. can be used to process with loop.
}
或者如果您可以更改输入字符串,则可以将这些对象放入 Json 数组。
[
{"status":"success","id":"1"},
{"status":"success","id":"2"},
{"status":"success","id":"3"},
{"status":"success","id":"4"},
{"status":"success","id":"5"},
{"status":"success","id":"6"}
]
这种情况下可以使用org.json.JSONArray来处理。
你可以像这样使用正则表达式
public class Test {
public static void main(String[] args) {
StringBuilder inputBuf = prepareStringBuilder();
StringBuilder outputBuf = new StringBuilder();
Pattern pattern = Pattern.compile(":\"(\d+)\"");
Matcher matcher = pattern.matcher(inputBuf);
while (matcher.find()) {
String group = matcher.group(1);
outputBuf.append(group);
}
System.out.println(outputBuf);
}
private static StringBuilder prepareStringBuilder() {
StringBuilder buf = new StringBuilder();
buf.append("{\"status\":\"success\",\"id\":\"1\"}");
buf.append("{\"status\":\"success\",\"id\":\"2\"}");
buf.append("{\"status\":\"success\",\"id\":\"3\"}");
buf.append("{\"status\":\"success\",\"id\":\"4\"}");
buf.append("{\"status\":\"success\",\"id\":\"5\"}");
buf.append("{\"status\":\"success\",\"id\":\"6\"}");
return buf;
}
}
我的 StringBuilder 变量中的值低于 json,我想解析所有 id 键值并将其再次存储在 StringBuilder 中。
{"status":"success","id":"1"}
{"status":"success","id":"2"}
{"status":"success","id":"3"}
{"status":"success","id":"4"}
{"status":"success","id":"5"}
{"status":"success","id":"6"}
预期输出: 1个 2个 3个 4个 5个 6
如何在 java 中解析这些值?
我尝试了以下选项,但没有帮助:
StringBuilder str = new StringBuilder();
str.append(jsonStringValue);
JSONObject jObj = new JSONObject(str);
jObj.getString("id");
使用 JSONTokener
JSONTokener t = new JSONTokener(str.toString());
while (t.more()) {
JSONObject o2 = (JSONObject) t.nextValue();
System.out.println(o2.getString("id"));
}
但我收到以下错误消息: org.json.JSONException:字符 128 处缺少值
如果您正在使用 org.json,您可以使用 JSONTokener。
下面的例子展示了它是如何工作的。
public static void main(String args[]) throws JSONException {
String str1 = "{\"strValue\":\"string\"}\n{\"intValue\":1}";
JSONTokener t = new JSONTokener(str1);
JSONObject o1 = (JSONObject) t.nextValue();
JSONObject o2 = (JSONObject) t.nextValue();
System.out.println(o1.getString("strValue"));
System.out.println(o2.getLong("intValue"));
System.out.println(t.more()); // Check if there's more token. can be used to process with loop.
}
或者如果您可以更改输入字符串,则可以将这些对象放入 Json 数组。
[
{"status":"success","id":"1"},
{"status":"success","id":"2"},
{"status":"success","id":"3"},
{"status":"success","id":"4"},
{"status":"success","id":"5"},
{"status":"success","id":"6"}
]
这种情况下可以使用org.json.JSONArray来处理。
你可以像这样使用正则表达式
public class Test {
public static void main(String[] args) {
StringBuilder inputBuf = prepareStringBuilder();
StringBuilder outputBuf = new StringBuilder();
Pattern pattern = Pattern.compile(":\"(\d+)\"");
Matcher matcher = pattern.matcher(inputBuf);
while (matcher.find()) {
String group = matcher.group(1);
outputBuf.append(group);
}
System.out.println(outputBuf);
}
private static StringBuilder prepareStringBuilder() {
StringBuilder buf = new StringBuilder();
buf.append("{\"status\":\"success\",\"id\":\"1\"}");
buf.append("{\"status\":\"success\",\"id\":\"2\"}");
buf.append("{\"status\":\"success\",\"id\":\"3\"}");
buf.append("{\"status\":\"success\",\"id\":\"4\"}");
buf.append("{\"status\":\"success\",\"id\":\"5\"}");
buf.append("{\"status\":\"success\",\"id\":\"6\"}");
return buf;
}
}