本文整理汇总了Java中javax.json.Json.createParser方法的典型用法代码示例。如果您正苦于以下问题:Java Json.createParser方法的具体用法?Java Json.createParser怎么用?Java Json.createParser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.json.Json
的用法示例。
在下文中一共展示了Json.createParser方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseJsonString
import javax.json.Json; //导入方法依赖的package包/类
private static void parseJsonString() {
JsonParser parser = Json.createParser(new StringReader(JSON));
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
switch (event) {
case START_ARRAY:
case END_ARRAY:
case START_OBJECT:
case END_OBJECT:
case VALUE_FALSE:
case VALUE_NULL:
case VALUE_TRUE:
System.out.println(event.toString());
break;
case KEY_NAME:
System.out.print(event.toString() + " " + parser.getString() + " - ");
break;
case VALUE_STRING:
case VALUE_NUMBER:
System.out.println(event.toString() + " " + parser.getString());
break;
}
}
}
示例2: retrieveValue
import javax.json.Json; //导入方法依赖的package包/类
public String retrieveValue(final String key_to_find, final String json) {
JsonParser parser = Json.createParser(new StringReader(json));
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
switch (event) {
case KEY_NAME:
if (parser.getString().equalsIgnoreCase(key_to_find)) {
event = parser.next();
if (event == VALUE_STRING || event == VALUE_NUMBER) {
return parser.getString();
}
}
break;
}
}
return null;
}
示例3: main
import javax.json.Json; //导入方法依赖的package包/类
public static void main(String... args) throws IOException, InterruptedException {
outputStream = new PipedOutputStream();
inputStream = new PipedInputStream(outputStream);
Thread thread1 = new Thread(() -> {
try {
outputStream.write("{".getBytes());
while(true) {
outputStream.write("\"colour\":\"red\",".getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
});
Thread thread2 = new Thread(() -> {
JsonParser parser = Json.createParser(inputStream);
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
System.out.println(event.toString());
if (event == KEY_NAME || event == VALUE_STRING || event == VALUE_NUMBER) {
System.out.println(parser.getString());
}
}
parser.close();
});
thread1.start();
Thread.sleep(5000);
thread2.start();
}