JSONArray object have getString(""); not found

时间:2015-06-26 10:00:39

标签: java json

Imported header files are:

import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

The line String name= jsonarray.getString("name");

causes the error: getString("") is not found

2 个答案:

答案 0 :(得分:3)

That's because JSONArray doesn't have a getString() method. For that matter, even a JSONObject doesn't have a getString() method.

We can only guess without knowing what your jsonArray contains, but my guess is you need,

String name= (String) ((JSONObject) jsonarray.get(0)).get("name");

EDIT: Based on your comment, I think your jsonarray is of type JSONObject (not very sure though), in that case use,

String name = (String) jsonarray.get("name");

答案 1 :(得分:0)

You are using org.json.simple.JSONArray API and there is no such method as getString(String str) or getString("")

Check org.json.JSONArray API for getString(int index).