2 years ago
#50705

Tirth Timaniya
JSONArray for array of String giving A JSONObject text must begin with '{' at 1 [character 2 line 1]
I am reading a JSON string and trying to get value for a given key. code to get value from JSON
private static void getKey(JSONObject jsonObject, String key) {
boolean exist = jsonObject.has(key);
Iterator<?> keys;
String nextKey;
if(!exist) {
keys = jsonObject.keys();
while (keys.hasNext()) {
nextKey = (String) keys.next();
System.out.println("Key is :: " + nextKey);
try {
if(jsonObject.get(nextKey) instanceof JSONObject) {
if(!exist) {
getKey(jsonObject.getJSONObject(nextKey), key);
}
} else if(jsonObject.get(nextKey) instanceof JSONArray) {
JSONArray jsonArray = jsonObject.getJSONArray(nextKey);
for(int i = 0; i < jsonArray.length(); i++) {
String arrayValue = jsonArray.get(i).toString();
JSONObject innerJSONObject = new JSONObject(arrayValue.trim());
if(!exist) {
getKey(innerJSONObject, key);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
parseJSONObject(jsonObject, key);
}
}
I am trying to parse this JSON String
String data = "[{\n" +
"\t\"siteName\": \"tvt-ieee\",\n" +
"\t\"personId\": \"43038888\",\n" +
"\t\"editorId\": \"43038888-tvt-ieee\",\n" +
"\t\"emails\": [{\n" +
"\t\t\"emailAddress\": \"02superjh@gmail.com\"\n" +
"\t}],\n" +
"\t\"keywords\": [\"H.1.2 User/Machine Systems\",\"I.2 Artificial Intelligence\"],\n" +
"}]";
JSONArray jsonObject = new JSONArray(data);
getKey(jsonObject.getJSONObject(0), "keywords");
but when I am trying to get value for key "keywords" it is throwing an error like A JSONObject text must begin with '{' at 1 [character 2 line 1]
please help me how can I solve this issue? Thank you in advance
java
json
jackson-databind
0 Answers
Your Answer