如何将符号列表响应从 fixer.io 转换为 json arraylist? (如何将 JSON 键值对转换为 JSON Array)
Posted
技术标签:
【中文标题】如何将符号列表响应从 fixer.io 转换为 json arraylist? (如何将 JSON 键值对转换为 JSON Array)【英文标题】:How to convert symbol list response into json arraylist from fixer.io? (How to convert JSON key value pairs into JSON Array) 【发布时间】:2021-06-18 03:11:33 【问题描述】:我想用currencyName
和currencyCode
显示货币列表,
但得到像
"success": true,
"symbols":
"AED": "United Arab Emirates Dirham",
"AFN": "Afghan Afghani"
我怎样才能把它转换成,
"success": true,
"symbols": [
"code": "AED",
"name": "United Arab Emirates Dirham"
,
"code": "AFN",
"name": "Afghan Afghani"
]
我想在 android 应用程序中显示列表。
【问题讨论】:
【参考方案1】:试试这个方法:
JSONObject resultObject = new JSONObject();
try
resultObject.put("success",true);
JSONArray jsonArray= new JSONArray();
JSONObject jsonObject=new JSONObject(yourJsonString);
JSONObject jsymbol= jsonObject.getJSONObject("symbols");
Iterator<String> iter = jsymbol.keys(); //This should be the iterator you want.
while(iter.hasNext())
String key = iter.next();
String value = (String) jsymbol.get(key);
JSONObject jsonObject1=new JSONObject();
jsonObject1.put("code",key);
jsonObject1.put("name",value);
jsonArray.put(jsonObject1);
resultObject.put("symbols",jsonArray);//This will give you result you want
catch (JSONException e)
e.printStackTrace();
【讨论】:
以上是关于如何将符号列表响应从 fixer.io 转换为 json arraylist? (如何将 JSON 键值对转换为 JSON Array)的主要内容,如果未能解决你的问题,请参考以下文章
如何将此 JSON 格式转换为模型类并使用改造将响应转换为列表