I'm currently parsing a JSON file to my Java Program and I ecounter a problem. I get a JSON array that can look like this for example:
[
{
"itemType": "magazineArticle",
"creators": [
{
"firstName": "J. Antonio",
"lastName": "Garcia-Macias",
"creatorType": "author"
},
{
"firstName": "Jorge",
"lastName": "Alvarez-Lozano",
"creatorType": "author"
},
{
"firstName": "Paul",
"lastName": "Estrada-Martinez",
"creatorType": "author"
},
{
"firstName": "Edgardo",
"lastName": "Aviles-Lopez",
"creatorType": "author"
}
],
"notes": [],
"tags": [],
"title": "Browsing the Internet of Things with Sentient Visors",
"publicationTitle": "Computer",
"volume": "44",
"issue": "5",
"ISSN": "0018-9162",
"date": "2011",
"pages": "46-52",
"abstractNote": "Unlike the traditional Internet, the emerging Internet of Things constitutes a mix of virtual and physical entities. A proposed IoT browser enables the exploration of augmented spaces by identifying smart objects, discovering any services they might provide, and interacting with them.",
"extra": "CICESE Research Center, Mexico; CICESE Research Center, Mexico; CICESE Research Center, Mexico; CICESE Research Center, Mexico",
"libraryCatalog": "IEEE Computer Society"
}
]
The problem is I have to do a check each time I parse if the field called "extra" is in the array, since it's not included every time i parse. So how do I do that check to see if the field "extra" exists?
Tomasz Jakub Rup
10.7k7 gold badges52 silver badges49 bronze badges
asked Dec 18, 2015 at 9:23
anderssinho
2962 gold badges8 silver badges23 bronze badges
-
1can't you use JSON parsing frameworks? Do you have to do it by hand?WeMakeSoftware– WeMakeSoftware2015年12月18日 09:25:06 +00:00Commented Dec 18, 2015 at 9:25
-
I have to do it by hand.anderssinho– anderssinho2015年12月18日 09:25:54 +00:00Commented Dec 18, 2015 at 9:25
-
Can you use Jackson or JsonSimple ?user3460409– user34604092015年12月18日 09:27:47 +00:00Commented Dec 18, 2015 at 9:27
-
show us your parsing codeWeMakeSoftware– WeMakeSoftware2015年12月18日 09:31:59 +00:00Commented Dec 18, 2015 at 9:31
-
Would prefer if I just could do a Simple check in my javaprogram actually. Like when I get the abstract field in my program I do: String abstracts = obj.getString("abstractNote"); And I would like to do is just to make an check when I try to get the field if the field actually exists.anderssinho– anderssinho2015年12月18日 09:32:14 +00:00Commented Dec 18, 2015 at 9:32
2 Answers 2
Without framework, you can use JSONObject and JSONArray from org.json.*. Here is an example (not tested). It will allows you to check if the extra key is present
String jsonAsString = "/*Your json as a String*/";
JsonArray jsonArray = new JSONArray(jsonAsString);
for(int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
//Get values from your jsonObject
jsonObject.has("extra"); //Check if extra is present
}
answered Dec 18, 2015 at 9:32
ThomasThiebaud
12.1k6 gold badges57 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use "has" method of JSONObject like this:
Array.getJSONObject(j).has("extra")
answered Dec 18, 2015 at 9:33
Grigoris Dimopoulos
1246 bronze badges
Comments
lang-java