Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从json对象获取字符串列表_Java_Arrays_Json_List - Fatal编程技术网

Java 如何从json对象获取字符串列表

Java 如何从json对象获取字符串列表,java,arrays,json,list,Java,Arrays,Json,List,我有以下JSON: {"errors":[{"code":888,"errorId":"xxx","message":"String value expected","fields":["name", "address"]}, {}, {}]} 我希望能够通过以下方式获取“字段”: public static String getField(json, errorsIndex, fieldIndex) { JSONObject errorJson = json.getJSONArray

我有以下JSON:

{"errors":[{"code":888,"errorId":"xxx","message":"String value expected","fields":["name", "address"]}, {}, {}]}
我希望能够通过以下方式获取“字段”:

public static String getField(json, errorsIndex, fieldIndex) {
    JSONObject errorJson = json.getJSONArray("errors").getJSONObject(errorIndex);
    String value = errorJson.[getTheListOfMyFields].get(fieldIndex);
    return value;
}

但是我找不到一种方法来制作这个部分[getTheListOfMyFields]。有什么建议吗?

您可以使用与访问错误数组相同的方式访问字段数组,而不是从JSON对象获取
列表:

public static String getField(JSONObject json, String errorsIndex, String fieldIndex) {
    JSONObject errorJson = json.getJSONArray("errors").getJSONObject(errorIndex);
    String value = errorJson.getJSONArray("fields").getString(fieldIndex);
    return value;
}

请注意,
get(fieldIndex)
已更改为
getString(fieldIndex)
。这样,您就不必将对象强制转换为字符串。

是否应该将
getTheListOfMyFields
作为索引?不,只是一个字符串列表(list):[“name”,“address”]最后,您希望
value
作为索引
fieldIndex
处的“fields”数组中的元素,对吗?例如,如果
fieldIndex
为1,那么
value
将为“address”?我认为JSONArray是一组json对象,在这里不适用。。。哈哈=)非常感谢!=)“json”参数的类型是什么,ha?@DynoCris它是包中的一个
JSONObject