java 使用java从动态json中查找键的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31157781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find value for a key from a dynamic json using java
提问by Chinmay Samant
I need to get the value of the key from a dynamic json.
Input-> json Object, String key
Output-> json element(corresponds to the value of the key)
Example
我需要从动态 json 中获取键的值。
Input-> json Object, String key
Output-> json element(对应key的值)
Example
JsonObject Jmsg =
JsonObject Jmsg =
{
"id": "1753_CORE1",
"name": "Gtx cuda Service:1753",
"shortName": "gt-service-1753",
"createdDate": "Mar 31, 2015 4:47:10 PM",
"config": {
"oauthSecret": [
{
"id": 45,
"config123": {
"oauthSecret": "P8n2x5Hsst0nFRRB0A",
"status": "CREATED"
},
"SERVER132": "1000"
},
{
"id": 46,
"config123": {
"oauthSecret": "P8n2x5Htss0nFRRB0A"
},
"SERVER132": "1000"
}
],
"oauthKey": "154284-service-1753",
"SERVER": "1000"
},
"features": [
9004,
9005
]
}
and String key = "status";
then
JsonElement Jvalue = jsonGetValueformKey(Jmsg,key);
should return 'CREATED' in JsonElement or string type.
和字符串键 = "状态";
然后 JsonElement Jvalue = jsonGetValueformKey(Jmsg,key);
应该以 JsonElement 或字符串类型返回 'CREATED'。
if String key = "features";
then
JsonElement Jvalue = jsonGetValueformKey(Jmsg,key);
should return [9004,9005] in JsonElement or jsonArray type.
if String key = "features";
然后
JsonElement Jvalue = jsonGetValueformKey(Jmsg,key);
应该以 JsonElement 或 jsonArray 类型返回 [9004,9005]。
if key not found then return null
JsonObject Jmsg can be anything
如果未找到键,则返回 null
JsonObject Jmsg 可以是任何东西
回答by Safwan Hijazi
please try this
请试试这个
package json;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class MyApp {
static List<String> list = new ArrayList<String>();
public static void main(String[] args) {
String key = "oauthSecret";
String json2 = "{\"config\": {\"oauthSecret\": [{\"id\": 45,\"config123\": {\"oauthSecret\": \"P8n2x5Ht0nFRRB0A\",\"status\": \"CREATED\"},\"SERVER132\": \"1000\"},{\"id\": 46,\"config123\": {\"oauthSecret\": \"wP8n2x5Ht0nFRRB0A\",\"status\": \"CREATED\"},\"SERVER132\": \"1000\"}],\"oauthKey\": \"newtest\",\"SERVER\": \"1000\"},\"features\": [ 9004, 9005] ,\"d\":\"dd\"}";
System.out.println("JSON: " + json2);
JsonParser p = new JsonParser();
check(key, p.parse(json2));
System.out.println("list size: " + list.size());
System.out.println(list);
}
private static void check(String key, JsonElement jsonElement) {
if (jsonElement.isJsonArray()) {
for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) {
check(key, jsonElement1);
}
} else {
if (jsonElement.isJsonObject()) {
Set<Map.Entry<String, JsonElement>> entrySet = jsonElement
.getAsJsonObject().entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
String key1 = entry.getKey();
if (key1.equals(key)) {
list.add(entry.getValue().toString());
}
check(key, entry.getValue());
}
} else {
if (jsonElement.toString().equals(key)) {
list.add(jsonElement.toString());
}
}
}
}
}
回答by DrB
This is a draft of a recursive approach for that.
这是一个递归方法的草案。
Object find(JSONObject jObj, String k) throws JSONException {
Iterator<?> keys = jObj.keys();
while( keys.hasNext() ) {
String key = (String)keys.next();
if (key.equals(k)) {
return jObj.get(key);
}
if ( jObj.get(key) instanceof JSONObject ) {
return find((JSONObject)jObj.get(key), k);
}
if ( jObj.get(key) instanceof JSONArray ) {
JSONArray jar = (JSONArray)jObj.get(key);
for (int i = 0; i < jar.length(); i++) {
JSONObject j = jar.getJSONObject(i);
find(j, k);
}
}
}
回答by Ankit Nigam
You could use something like this :-
你可以使用这样的东西:-
public Object checkKey(JSONObject object, String searchedKey) {
boolean exists = object.containsKey(searchedKey);
Object obj = null;
if(exists){
obj = object.get(searchedKey);
}
if(!exists) {
Set<String> keys = object.keySet();
for(String key : keys){
if ( object.get(key) instanceof JSONObject ) {
obj = checkKey((JSONObject)object.get(key), searchedKey);
}
}
}
return obj;
}
This will give you the Object type for a key OR null if key does not exists.
You can modify it & caste the return Object
type to any JSONObject
, String
or JSONArray
depending upon your condition by checking its class using getClass()
.
如果键不存在,这将为您提供键或 null 的对象类型。您可以修改它并将返回Object
类型强制转换为 any JSONObject
,String
或者JSONArray
通过使用getClass()
.
Note :- This is just a reference but you can edit it according to your needs.
注意:- 这只是一个参考,但您可以根据需要进行编辑。