如何在Java中从JSONObject中获取键和值。

huangapple go评论67阅读模式
英文:

How to get the key and value fr a JSONObject in java

问题

我们有一个 JSON 请求,我需要检查一些条件。

  1. 通过从键获取值。(例如:从键“Name”获取“Antony”)
  2. 检查 JSON 对象中是否存在该键。(检查下面对象中是否存在 Dob 键)

JSON 请求的格式如下。

"School": {"RollNum": "123", "Name": "Antony", "Address": "India"}

请有人帮我提供解决方案。

英文:

We have a JSON request and I need to check some conditions.

  1. by getting the value from a key.(eg. Getting Antony from the key Name)
  2. checking whether the key is there in json object or not. (Check Dob key is there or not in the below object)

JSON request would be in the below format.

"School":{"RollNum":"123","Name":"Antony","Address":"India"}

Please someone help me with the solution.

答案1

得分: 1

你可以尝试这种方式。

JsonObject jsonObject = (JsonObject) JsonParser.parseString("yourJason");
String name = jsonObject.get("Name").getAsString();

如果你的 JSON 包含嵌套对象,那么可以尝试这样做。

JsonObject school = jsonObject.get("School");
String name = school.get("Name").getAsString();
英文:

you can try this way.

JsonObject jsonObject = (JsonObject) JsonParser.parseString("yourJason");
String name = jsonObject.get("Name").getAsString();

If your json have nexted object, than you can try.

JsonObject school = jsonObject.get("School");
String name = school.get("Name").getAsString();

答案2

得分: 1

我向您推荐使用库Gson

对于Maven:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
  <scope>compile</scope>
</dependency>

简单示例:

Gson gson = new Gson();

JsonObject json = gson.fromJson("your json string", JsonObject.class);

// 获取属性
String name = json.get("Name").getAsString();

// 检查属性
if(json.has("someKeyName")) {
  // 做其他操作
}
英文:

I recommend the library Gson to you.

For Maven:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
  <scope>compile</scope>
</dependency>

Simple Example:

Gson gson = new Gson();

JsonObject json = gson.fromJson("your json string", JsonObject.class);

// get attribute
String name = json.get("Name").getAsString();

// check an attribute
if(json.has("someKeyName")) {
  // do something else
}

答案3

得分: 1

获取属性,请使用以下代码:

String key = jsonObject.get("key").getAsString();

检查属性:

jsonObject.has(key) && !jsonObject.isNull(key);  //key将是您要检查的字段

如果您还想检查字符串不为空,可以使用以下代码:

jsonObject.has(key) && !jsonObject.isNull(key) && !jsonObject.get(key).getAsString().isEmpty();
英文:

To get an attribute, use this:

String key = jsonObject.get("key").getAsString();

To check an attribute:

jsonObject.has(key) && !jsonObject.isNull(key);  //key will be your field to check

If you want to check if the string is not empty as well, use this:

jsonObject.has(key) && !jsonObject.isNull(key) && jsonObject.has(key) && !jsonObject.isNull(key);

huangapple
  • 本文由 发表于 2020年10月23日 17:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定