本文整理了Java中javax.json.Json
类的一些代码示例,展示了Json
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Json
类的具体详情如下:
包路径:javax.json.Json
类名称:Json
[英]Factory class for creating JSON processing objects. This class provides the most commonly used methods for creating these objects and their corresponding factories. The factory classes provide all the various ways to create these objects.
The methods in this class locate a provider instance using the method JsonProvider#provider(). This class uses the provider instance to create JSON processing objects.
The following example shows how to create a JSON parser to parse an empty array:
StringReader reader = new StringReader("[]");
JsonParser parser = Json.createParser(reader);
All the methods in this class are safe for use by multiple concurrent threads.
[中]用于创建JSON处理对象的工厂类。此类提供了创建这些对象及其相应工厂的最常用方法。工厂类提供了创建这些对象的各种方法。
此类中的方法使用方法JsonProvider#provider()定位提供程序实例。此类使用提供程序实例创建JSON处理对象。
以下示例显示如何创建JSON解析器来解析空数组:
StringReader reader = new StringReader("[]");
JsonParser parser = Json.createParser(reader);
此类中的所有方法对于多个并发线程都是安全的。
代码示例来源:origin: stanfordnlp/CoreNLP
public static Map<String, Set<CandidatePhrase>> readSeedWordsFromJSONString(String str){
Map<String, Set<CandidatePhrase>> seedWords = new HashMap<>();
JsonReader jsonReader = Json.createReader(new StringReader(str));
JsonObject obj = jsonReader.readObject();
jsonReader.close();
for (String o : obj.keySet()){
seedWords.put(o, new HashSet<>());
JsonArray arr = obj.getJsonArray(o);
for(JsonValue v: arr)
seedWords.get(o).add(CandidatePhrase.createOrGet(v.toString()));
}
return seedWords;
}
代码示例来源:origin: stanfordnlp/CoreNLP
private static JsonArrayBuilder jsonArrayBuilderFromMapCounter(Map<String, List<Integer>> mapCounter) {
JsonArrayBuilder arrobj = Json.createArrayBuilder();
for (Entry<String, List<Integer>> sen : mapCounter.entrySet()) {
JsonObjectBuilder obj = Json.createObjectBuilder();
JsonArrayBuilder tokens = Json.createArrayBuilder();
for(Integer i : sen.getValue()){
tokens.add(i);
}
obj.add(sen.getKey(),tokens);
arrobj.add(obj);
}
return arrobj;
}
代码示例来源:origin: jeremylong/DependencyCheck
final JsonObjectBuilder payloadBuilder = Json.createObjectBuilder();
payloadBuilder.add("name", packageJson.getString("name"));
payloadBuilder.add("version", packageJson.getString("version"));
final JsonValue jsonValue = packageJson.get("requires");
if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
final JsonObjectBuilder requiresBuilder = Json.createObjectBuilder();
final JsonObject dependencies = packageJson.getJsonObject("dependencies");
for (String moduleName: dependencies.keySet()) {
final JsonObject module = dependencies.getJsonObject(moduleName);
final String version = module.getString("version");
requiresBuilder.add(moduleName, version);
payloadBuilder.add("requires", requiresBuilder.build());
payloadBuilder.add("dependencies", packageJson.getJsonObject("dependencies"));
payloadBuilder.add("install", Json.createArrayBuilder().build());
payloadBuilder.add("remove", Json.createArrayBuilder().build());
payloadBuilder.add("metadata", Json.createObjectBuilder()
.add("npm_version", "6.1.0")
.add("node_version", "v10.5.0")
代码示例来源:origin: IQSS/dataverse
protected Response ok( String msg ) {
return Response.ok().entity(Json.createObjectBuilder()
.add("status", STATUS_OK)
.add("data", Json.createObjectBuilder().add("message",msg)).build() )
.type(MediaType.APPLICATION_JSON)
.build();
}
代码示例来源:origin: wildfly/wildfly
public static JsonObject readJsonObject(String jsonString) {
return Json.createReader(new StringReader(jsonString)).readObject();
}
代码示例来源:origin: wildfly/wildfly
public static JsonArray readJsonArray(String jsonString) {
return Json.createReader(new StringReader(jsonString)).readArray();
}
代码示例来源:origin: stanfordnlp/CoreNLP
private int changeAnnotation(String line, boolean remove){
int tokensNum = 0;
JsonReader jsonReader = Json.createReader(new StringReader(line));
JsonObject objarr = jsonReader.readObject();
for(String label: objarr.keySet()) {
JsonObject obj4label = objarr.getJsonObject(label);
for(String sentid: obj4label.keySet()){
JsonArray tokenArry = obj4label.getJsonArray(sentid);
for(JsonValue tokenid: tokenArry){
tokensNum ++;
Data.sents.get(sentid).getTokens().get(Integer.parseInt(tokenid.toString())).set(humanLabelClasses.get(label), remove ? backgroundSymbol: label);
}
}
}
return tokensNum;
}
代码示例来源:origin: stanfordnlp/CoreNLP
void setUpProperties(String line, boolean readFile, boolean writeOutputToFile, String additionalSeedWordsFiles) throws IOException, ClassNotFoundException {
JsonReader jsonReader = Json.createReader(new StringReader(line));
JsonObject objarr = jsonReader.readObject();
jsonReader.close();
Properties props = new Properties();
for (String o : objarr.keySet()){
if(o.equals("seedWords")){
JsonObject obj = objarr.getJsonObject(o);
for (String st : obj.keySet()){
seedWords.put(st, new HashSet<>());
JsonArray arr = obj.getJsonArray(st);
代码示例来源:origin: stanfordnlp/CoreNLP
public String doNewPhrases(String line) throws Exception {
System.out.println("adding new phrases");
ConstantsAndVariables constVars = new ConstantsAndVariables(props, humanLabelClasses.keySet(), humanLabelClasses);
JsonReader jsonReader = Json.createReader(new StringReader(line));
JsonObject objarr = jsonReader.readObject();
for(Map.Entry<String, JsonValue> o: objarr.entrySet()){
String label = o.getKey();
Set<CandidatePhrase> seed = new HashSet<>();
JsonArray arr = objarr.getJsonArray(o.getKey());
for(int i = 0; i < arr.size(); i++){
String seedw = arr.getString(i);
System.out.println("adding " + seedw + " to seed ");
seed.add(CandidatePhrase.createOrGet(seedw));
}
seedWords.get(label).addAll(seed);
constVars.addSeedWords(label, seed);
GetPatternsFromDataMultiClass.runLabelSeedWords(Data.sents, humanLabelClasses.get(label), label, seed, constVars, false);
//model.labelWords(label, labelclass, Data.sents, seed);
}
return "SUCCESS added new phrases";
}
代码示例来源:origin: org.microprofile-ext.config-ext/configsource-memory
private Response allForConfigSourceToJson(String configsource) {
if(configSourceMap.containsKey(configsource)){
ConfigSource source = configSourceMap.get(configsource);
Set<Map.Entry<String, String>> propertiesSet = source.getProperties().entrySet();
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
for(Map.Entry<String, String> propertyEntry:propertiesSet){
arrayBuilder.add(Json.createObjectBuilder().add(propertyEntry.getKey(), propertyEntry.getValue()).build());
}
return Response.ok(arrayBuilder.build()).build();
}
return Response.noContent().header(REASON, NO_SUCH_CONFIGSOURCE).build();
}
代码示例来源:origin: wildfly/wildfly
JsonReader reader = Json.createReader(
new StringReader(builder.toString()));
JsonValue value = reader.readValue();
reader.close();
return value;
代码示例来源:origin: apache/nifi
JsonObjectBuilder builder = Json.createObjectBuilder();
String text = context.getProperty(WEBHOOK_TEXT).evaluateAttributeExpressions(flowFile).getValue();
if (text != null && !text.isEmpty()) {
builder.add("text", text);
} else {
String error = validateChannel(channel);
if (error == null) {
builder.add("channel", channel);
} else {
getLogger().error("Invalid channel '{}': {}", new Object[]{channel, error});
builder.add("username", username);
JsonArrayBuilder jsonArrayBuiler = Json.createArrayBuilder();
for (PropertyDescriptor attachment : attachments) {
String s = context.getProperty(attachment).evaluateAttributeExpressions(flowFile).getValue();
JsonReader reader = Json.createReader(new StringReader(s));
JsonObject attachmentJson = reader.readObject();
jsonArrayBuiler.add(attachmentJson);
builder.add("attachments", jsonArrayBuiler);
JsonObject jsonObject = builder.build();
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = Json.createWriter(stringWriter);
jsonWriter.writeObject(jsonObject);
jsonWriter.close();
代码示例来源:origin: traccar/traccar
JsonObject root = Json.createReader(
new StringReader(request.content().toString(StandardCharsets.US_ASCII))).readObject();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, root.getString("IMEI"));
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
JsonArray records = root.getJsonArray("Records");
position.set(Position.KEY_INDEX, record.getInt("SeqNo"));
position.set(Position.KEY_EVENT, record.getInt("Reason"));
代码示例来源:origin: traccar/traccar
JsonObject root = Json.createReader(
new StringReader(request.content().toString(StandardCharsets.US_ASCII))).readObject();
if (!root.containsKey("_type")) {
sendResponse(channel, HttpResponseStatus.OK);
return null;
if (!root.getString("_type").equals("location")
&& !root.getString("_type").equals("lwt")) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
代码示例来源:origin: hyperledger/fabric-sdk-java
JsonReader reader = Json.createReader(new StringReader(responseBody));
JsonObject jobj = (JsonObject) reader.read();
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("statusCode", respStatusCode);
JsonArray errors = jobj.getJsonArray("errors");
JsonObject jo = errors.getJsonObject(0);
String errorMsg = format("[HTTP Status Code: %d] - %s request to %s failed request body %s error message: [Error Code %d] - %s",
respStatusCode, type, url, body, jo.getInt("code"), jo.getString("message"));
logger.error(errorMsg);
return job.build();
for (Entry<String, JsonValue> entry : result.entrySet()) {
job.add(entry.getKey(), entry.getValue());
job.add("statusCode", respStatusCode);
代码示例来源:origin: stanfordnlp/CoreNLP
JsonReader json = Json.createReader(new StringReader(jsonString));
SemanticGraph tree = new SemanticGraph();
JsonArray array = json.readArray();
JsonObject entry = array.getJsonObject(i);
int dependentIndex = entry.getInt("dependent");
if (vertices[dependentIndex] == null) {
if (dependentIndex > tokens.size()) {
int governorIndex = entry.getInt("governor");
if (governorIndex > tokens.size()) {
String relation = entry.getString("dep");
代码示例来源:origin: hyperledger/fabric-sdk-java
JsonObjectBuilder factory = Json.createObjectBuilder();
factory.add(HFCAClient.FABRIC_CA_REQPROP, caName);
JsonObject body = factory.build();
JsonReader reader = Json.createReader(new StringReader(responseBody));
JsonObject jsonst = (JsonObject) reader.read();
boolean success = jsonst.getBoolean("success");
logger.debug(format("[HFCAClient] enroll success:[%s]", success));
JsonObject result = jsonst.getJsonObject("result");
if (result == null) {
throw new InfoException(format("FabricCA info error - response did not contain a result url %s", url));
String caName = result.getString("CAName");
String caChain = result.getString("CAChain");
String version = null;
代码示例来源:origin: traccar/traccar
JsonObject json = Json.createReader(new StringReader(URLDecoder.decode(
request.content().toString(StandardCharsets.UTF_8).split("=")[0], "UTF-8"))).readObject();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, json.getString("device"));
if (deviceSession == null) {
sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(json.getInt("time") * 1000L));
ByteBuf buf = Unpooled.wrappedBuffer(DataConverter.parseHex(json.getString("data")));
try {
int type = buf.readUnsignedByte() >> 4;
代码示例来源:origin: traccar/traccar
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
FullHttpRequest request = (FullHttpRequest) msg;
JsonArray result = Json.createReader(new StringReader(request.content().toString(StandardCharsets.UTF_8)))
.readArray();
List<Position> positions = new LinkedList<>();
for (int i = 0; i < result.size(); i++) {
JsonObject message = result.getJsonObject(i);
JsonString ident = message.getJsonString("ident");
if (ident == null) {
continue;
}
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString());
if (deviceSession == null) {
continue;
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
decodePosition(message, position);
positions.add(position);
}
sendResponse(channel, HttpResponseStatus.OK);
return positions;
}
代码示例来源:origin: IQSS/dataverse
public static JsonArrayBuilder stringToJsonArrayBuilder(String str) {
JsonReader jsonReader = Json.createReader(new StringReader(str));
JsonArray ja = jsonReader.readArray();
jsonReader.close();
JsonArrayBuilder jab = Json.createArrayBuilder();
for (int i = 0; i < ja.size(); i++) {
JsonObjectBuilder job = Json.createObjectBuilder();
for (Map.Entry<String, JsonValue> entry : ja.getJsonObject(i).entrySet()) {
job.add(entry.getKey(), entry.getValue());
}
jab.add(job);
}
return jab;
}
}
内容来源于网络,如有侵权,请联系作者删除!