简体   繁体   English

将 Json 字符串转换为 JSONArray

[英]Convert a Json String into JSONArray

I'm trying to create test data with a JSON String, however when I try to convert the string into a JSONArray, the test fails.我正在尝试使用 JSON 字符串创建测试数据,但是当我尝试将字符串转换为 JSONArray 时,测试失败。

    String JSONString = "{\"Info\":[{\"area\":301336,\"nativeName\":\"Italia\",\"capital\":\"Rome\",\"demonym\":\"Italian\",\"flag\":\"https://restcountries.eu/data/ita.svg\",\"alpha2Code\":\"IT\",\"languages\":[{\"nativeName\":\"Italiano\",\"iso639_2\":\"ita\",\"name\":\"Italian\",\"iso639_1\":\"it\"}],\"borders\":[\"AUT\",\"FRA\",\"SMR\",\"SVN\",\"CHE\",\"VAT\"],\"subregion\":\"Southern Europe\",\"callingCodes\":[\"39\"],\"regionalBlocs\":[{\"otherNames\":[],\"acronym\":\"EU\",\"name\":\"European Union\",\"otherAcronyms\":[]}],\"gini\":36,\"population\":60665551,\"numericCode\":\"380\",\"alpha3Code\":\"ITA\",\"topLevelDomain\":[\".it\"],\"timezones\":[\"UTC+01:00\"],\"cioc\":\"ITA\",\"translations\":{\"br\":\"Itália\",\"de\":\"Italien\",\"pt\":\"Itália\",\"ja\":\"イタリア\",\"hr\":\"Italija\",\"it\":\"Italia\",\"fa\":\"ایتالیا\",\"fr\":\"Italie\",\"es\":\"Italia\",\"nl\":\"Italië\"},\"name\":\"Italy\",\"altSpellings\":[\"IT\",\"Italian Republic\",\"Repubblica italiana\"],\"region\":\"Europe\",\"latlng\":[42.83333333,12.83333333],\"currencies\":[{\"symbol\":\"\\u20ac\",\"code\":\"EUR\",\"name\":\"Euro\"}]}]}";
    JSONArray JSON = new JSONArray(JSONString);     

The error is org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]错误是 org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

A JSONArray must starts always with [ JSONArray必须始终以[开头

You must to change your JSONString variable as follow:您必须按如下方式更改 JSONString 变量:

Solution 1:解决方案1:

[Info... and so on [Info...等等

Solution 2:解决方案2:

Remove Info because it is typized element of your JSON array删除Info因为它是 JSON 数组的典型元素

Your string represents a JSON Object, starting with "{".您的字符串代表一个 JSON 对象,以“{”开头。 A JSONArray would begin with a "[". JSONArray 将以“[”开头。

If you parse the string into a JSONObject first, you should then be able to parse out your JSONArray from the "Info" key.如果您首先将字符串解析为 JSONObject,那么您应该能够从“Info”键解析出您的 JSONArray。

Just to elaborate on this in your case, something like this:只是为了详细说明您的情况,如下所示:

    String JSONString = "{\"Info\":[{\"area\":301336,\"nativeName\":\"Italia\",\"capital\":\"Rome\",\"demonym\":\"Italian\",\"flag\":\"https://restcountries.eu/data/ita.svg\",\"alpha2Code\":\"IT\",\"languages\":[{\"nativeName\":\"Italiano\",\"iso639_2\":\"ita\",\"name\":\"Italian\",\"iso639_1\":\"it\"}],\"borders\":[\"AUT\",\"FRA\",\"SMR\",\"SVN\",\"CHE\",\"VAT\"],\"subregion\":\"Southern Europe\",\"callingCodes\":[\"39\"],\"regionalBlocs\":[{\"otherNames\":[],\"acronym\":\"EU\",\"name\":\"European Union\",\"otherAcronyms\":[]}],\"gini\":36,\"population\":60665551,\"numericCode\":\"380\",\"alpha3Code\":\"ITA\",\"topLevelDomain\":[\".it\"],\"timezones\":[\"UTC+01:00\"],\"cioc\":\"ITA\",\"translations\":{\"br\":\"Itália\",\"de\":\"Italien\",\"pt\":\"Itália\",\"ja\":\"イタリア\",\"hr\":\"Italija\",\"it\":\"Italia\",\"fa\":\"ایتالیا\",\"fr\":\"Italie\",\"es\":\"Italia\",\"nl\":\"Italië\"},\"name\":\"Italy\",\"altSpellings\":[\"IT\",\"Italian Republic\",\"Repubblica italiana\"],\"region\":\"Europe\",\"latlng\":[42.83333333,12.83333333],\"currencies\":[{\"symbol\":\"\\u20ac\",\"code\":\"EUR\",\"name\":\"Euro\"}]}]}";

    JSONObject jsonObject = new JSONObject(JSONString);
    JSONArray jsonArray = jsonObject.getJSONArray("Info");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM