我有一个JSONArray,如果此JSONArray中有特定的键值可用,则需要获取。可以说,我有一个特定的键值对,例如 Current Employment Status:False ,我需要验证该键值对在JSONArray中是否可用。我的JSONArray如下:
[
{
"last_seen":1557908766238,
"distinct_id":"11cb639f-cbf7-499c- a1cf-e3ff4466a093",
"time":1557908766238,
"properties":{
"$browser_version":74,
"$name":"Jessica Claire",
"$timezone":"Asia/Kolkata",
"$os":"Windows",
"$email":"19_05_15_135529631_ebMPR@email-qa.livecareer.com",
"$initial_referring_domain":"$direct",
"userid":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
"$first_name":"Jessica",
"Current Employment Status":"FALSE",
"enter builder":1,
"fedex experience":"FALSE",
"ResumeTips Permission":"TRUE",
"id":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
"LCNews Permission":"TRUE",
"Document Creation Mode":"scratch",
"Email Valid":"False",
"$last_name":"Claire",
"Payment Status":"Not Started",
"Work Experience Modal":"TRUE",
"$city":"Noida",
"Subscription Status":"FALSE",
"Job Alerts Permission":"TRUE",
"sign up":1,
"$browser":"Chrome",
"userId":"11cb639f-cbf7-499c-a1cf-e3ff4466a093",
"$country_code":"IN",
"$region":"Uttar Pradesh",
"One Time Purchase":"FALSE",
"$last_seen":"2019-05-15T08:26:06.000Z",
"Last enter builder":"2019-05-15T08:24:01.000Z",
"Last sign up":"2019-05-15T08:26:03.000Z",
"$initial_referrer":"$direct",
"EduOps Permission":"TRUE"
},
"labels":[
]
}
]
如果存在,则应打印true;否则,则应打印false。
答案 0 :(得分:1)
您可以使用JsonArray和JsonObject来做到这一点。使用org.json maven依赖项。
import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws IOException, JSONException {
String sb = "[{\"last_seen\":1557908766238,\"distinct_id\":\"11cb639f-cbf7-499c- a1cf-e3ff4466a093\",\"time\":1557908766238,\"properties\": {\"$browser_version\":74,\"$name\":\"Jessica Claire\",\"$timezone\":\"Asia/Kolkata\",\"$os\":\"Windows\",\"$email\":\"19_05_15_135529631_ebMPR@email-qa.livecareer.com\",\"$initial_referring_domain\":\"$direct\",\"userid\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$first_name\":\"Jessica\",\"Current Employment Status\":\"FALSE\",\"enter builder\":1,\"fedex experience\":\"FALSE\",\"ResumeTips Permission\":\"TRUE\",\"id\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"LCNews Permission\":\"TRUE\",\"Document Creation Mode\":\"scratch\",\"Email Valid\":\"False\",\"$last_name\":\"Claire\",\"Payment Status\":\"Not Started\",\"Work Experience Modal\":\"TRUE\",\"$city\":\"Noida\",\"Subscription Status\":\"FALSE\",\"Job Alerts Permission\":\"TRUE\",\"sign up\":1,\"$browser\":\"Chrome\",\"userId\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$country_code\":\"IN\",\"$region\":\"Uttar Pradesh\",\"One Time Purchase\":\"FALSE\",\"$last_seen\":\"2019-05-15T08:26:06.000Z\",\"Last enter builder\":\"2019-05-15T08:24:01.000Z\",\"Last sign up\":\"2019-05-15T08:26:03.000Z\",\"$initial_referrer\":\"$direct\",\"EduOps Permission\":\"TRUE\"},\"labels\":[]}]";
JSONArray jsonArray = new JSONArray(sb);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Object value = null;
try {
JSONObject properties = (JSONObject) jsonObject.get("properties");
value = properties.get("Current Employment Status");
} catch (JSONException e) {
//print flase if not found
System.out.println("false");
}
//print the value if found
System.out.println("Value =" + value);
}
}
}
答案 1 :(得分:1)
我已将此json存储在名为findKey.json的文件中
String path = "others\\findKey.json";
File f = new File(path);
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(f));
JSONArray arr = (JSONArray) obj;
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) instanceof JSONObject) {
JSONObject jsonobject = (JSONObject) arr.get(i);
JSONObject props = (JSONObject) jsonobject.get("properties");
String key = (String) props.get("Current Employment Status");
//print true if key is found and equals to false
Boolean output= (key != null && key.equals("FALSE"))? true:false;
System.out.println(output);
}
}
别忘了使用json-simple
答案 2 :(得分:0)
我用过罐子将String解析为JSon
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class test {
public static void main(String[] args) {
String temp = "[{\"last_seen\":1557908766238,\"distinct_id\":\"11cb639f-cbf7-499c- "
+ "a1cf-e3ff4466a093\",\"time\":1557908766238,\"properties\": {\"$browser_version\":74,"
+ " \" $name\":\"Jessica Claire\",\"$timezone\":\"AsiaKolkata\","
+ "\"$os\":\"Windows\",\"$email\":\"19_05_15_135529631_ebMPR@email-qa.livecareer.com\",\"$initial_referring_domain\":\"$direct\",\"userid\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$first_name\":\"Jessica\",\"Current Employment Status\":\"FALSE\",\"enter builder\":1,\"fedex experience\":\"FALSE\",\"ResumeTips Permission\":\"TRUE\",\"id\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"LCNews Permission\":\"TRUE\",\"Document Creation Mode\":\"scratch\",\"Email Valid\":\"False\",\"$last_name\":\"Claire\",\"Payment Status\":\"Not Started\",\"Work Experience Modal\":\"TRUE\",\"$city\":\"Noida\",\"Subscription Status\":\"FALSE\",\"Job Alerts Permission\":\"TRUE\",\"sign up\":1,\"$browser\":\"Chrome\",\"userId\":\"11cb639f-cbf7-499c-a1cf-e3ff4466a093\",\"$country_code\":\"IN\",\"$region\":\"Uttar Pradesh\",\"One Time Purchase\":\"FALSE\",\"$last_seen\":\"2019-05-15T08:26:06.000Z\",\"Last enter builder\":\"2019-05-15T08:24:01.000Z\",\"Last sign up\":\"2019-05-15T08:26:03.000Z\",\"$initial_referrer\":\"$direct\",\"EduOps Permission\":\"TRUE\"},\"labels\":[]}]";
String findkeyvalue = "Current Employment Status:False";
boolean flag = FindValue(temp , findkeyvalue);
System.out.println(flag);
}
private static boolean FindValue(String temp, String findkeyvalue) {
try {
JSONParser jsonParser = new JSONParser();
JSONArray array = (JSONArray) jsonParser.parse(temp);
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i).toString());
return travertillLast(array.get(i).toString() , findkeyvalue);
}
} catch (Exception e) {
return false;
}
return false;
}
private static boolean travertillLast(String string, String findkeyvalue) throws ParseException {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParser.parse(string);
System.out.println(jsonObj);
for (Object key : jsonObj.keySet()) {
//based on you key types
String keyStr = (String)key;
Object keyvalue = jsonObj.get(keyStr);
//Print key and value
String keyandValuue = keyStr + ":" + keyvalue;
System.out.println(keyandValuue);
if(keyandValuue.trim().equalsIgnoreCase(findkeyvalue)){
return true;
}
//for nested objects iteration if required
if (keyvalue instanceof JSONObject)
return travertillLast(keyvalue+"",findkeyvalue);
}
return false;
// TODO Auto-generated method stub
}
}