如何在Android kotlin,Moshi和Retrofit中使用 NESTED 动态键解析嵌套JSON?
我从alpha-vantage获取此JSON。
格式示例:
{
"Meta Data": {
"1. Information": "Intraday (15min) open, high, low, close prices and volume",
"2. Symbol": "AAME",
"3. Last Refreshed": "2019-11-18 16:00:00",
"4. Interval": "15min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (15min)": {//Dynamic - > Time Series (5min) / Time Series (30min)
"2019-11-18 16:00:00": {//Dynamic
"1. open": "1.6700",
"2. high": "1.6700",
"3. low": "1.5700",
"4. close": "1.5700",
"5. volume": "1521"
},
"2019-11-18 15:45:00": {//Dynamic
"1. open": "1.6600",
"2. high": "1.7400",
"3. low": "1.6600",
"4. close": "1.7400",
"5. volume": "355"
}
}
}
我尝试使用自定义适配器,但是找不到用它解析双嵌套动态键的方法。 现在,我使用手动解析:
fun convertJsonToItemDetails(jso: JSONObject) {
val meta: JSONObject? = jso.optJSONObject("Meta Data")
var metaData: ItemMetaData? = null
meta?.apply {
val information = optString("1. Information")
val symbol = optString("2. Symbol")
val lastRefreshed = optString("3. Last Refreshed")
val interval = optString("4. Interval")
val outputSize = optString("5. Output Size")
val timeZone = optString("6. Time Zone")
metaData =
ItemMetaData(information, symbol, lastRefreshed, interval, outputSize, timeZone)
}
if (metaData == null) {
//TODO return error object
return
}
val timeSeriesJSON = jso.optJSONObject("Time Series (${metaData?.interval})")
val timeSeires = HashMap<String, IntervalOutput>()
if (timeSeriesJSON == null) {
//TODO return error object
return
}
for (key in timeSeriesJSON.keys()) {
val intervalOutPutJSON = timeSeriesJSON.getJSONObject(key)
val open = intervalOutPutJSON.getString("1. open")
val high = intervalOutPutJSON.getString("2. high")
val low = intervalOutPutJSON.getString("3. low")
val close = intervalOutPutJSON.getString("4. close")
val volume = intervalOutPutJSON.getString("5. volume")
timeSeires[key] = IntervalOutput(open, high, low, close, volume)
}
val itemDetails = ItemDetails(metaData, timeSeires)
_itemDetails.value = itemDetails
}
答案 0 :(得分:1)
只需将您的json字符串传递给此方法,便可以正常使用。
private void parseJson(String jsonString) {
try {
// pass your response json string here
JSONObject object = new JSONObject(jsonString);
Iterator<String> it = object.keys();
while (it.hasNext()) {
String key = it.next();
try {
if (object.get(key) instanceof JSONObject) {
Log.e("Main OBJ", key);
JSONObject object2 = object.getJSONObject(key);
Iterator<String> it2 = object2.keys();
while (it2.hasNext()) {
String key2 = it2.next();
if (object2.get(key2) instanceof JSONObject) {
Log.e("Sub OBJ", key2);
JSONObject object3 = object2.getJSONObject(key2);
Iterator<String> it3 = object3.keys();
while (it3.hasNext()) {
String key4 = it3.next();
Log.e("Values", key4);
}
} else {
Log.e("Values", object2.getString(key2));
}
}
} else {
System.out.println(key + ":" + object.getString(key));
}
} catch (Throwable e) {
try {
System.out.println(key + ":" + object.getString(key));
} catch (Exception ee) {
ee.printStackTrace();
}
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
输出:
Main OBJ: Meta Data
Values: Intraday (15min) open, high, low, close prices and volume
Values: AAME
Values: 2019-11-18 16:00:00
Values: 15min
Values: Compact
Values: US/Eastern
Main OBJ: Time Series (15min)
Sub OBJ: 2019-11-18 16:00:00
Values: 1. open
Values: 2. high
Values: 3. low
Values: 4. close
Values: 5. volume
Sub OBJ: 2019-11-18 15:45:00
Values: 1. open
Values: 2. high
Values: 3. low
Values: 4. close
Values: 5. volume
答案 1 :(得分:0)
使用JSONObject keys()获取Dynamic Keys
,然后使用Iterate
每个特定键获取动态值。
打印第一层的所有JSONObject
的时间很短,如下所示。
var dynamicJSON: String = {
"Meta Data": {
"1. Information": "Intraday (15min) open, high, low, close prices and volume",
"2. Symbol": "AAME",
"3. Last Refreshed": "2019-11-18 16:00:00",
"4. Interval": "15min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (15min)": {//Dynamic - > Time Series (5min) / Time Series (30min)
"2019-11-18 16:00:00": {//Dynamic
"1. open": "1.6700",
"2. high": "1.6700",
"3. low": "1.5700",
"4. close": "1.5700",
"5. volume": "1521"
},
"2019-11-18 15:45:00": {//Dynamic
"1. open": "1.6600",
"2. high": "1.7400",
"3. low": "1.6600",
"4. close": "1.7400",
"5. volume": "355"
}
}
}
然后,您必须开始将其解析为JSONObject,然后对JSONObject "Meta Data"
和"Time Series (15min)"
进一步执行相同的过程
val dynamicjson: JSONObject = JSONObject(dynamicJSON)
val keys: Iterator<*> = dynamicjson.keys()
while (keys.hasNext()) { // loop to get the dynamic key
val currentDynamicKey = keys.next() as String
// get the value of the dynamic key
val currentDynamicValue: JSONObject = dynamicjson.getJSONObject(currentDynamicKey)
// do something here with the value... or either make another while loop to Iterate further
Log.e("JSON Value", currentDynamicValue.toString())
}
当我执行代码时,输出看起来像这样:
2019-12-12 15:21:08.399 19798-19798 / com.animusabhi.dynamicjsonparsing E / JSON值:{“ 1.信息”:“当日(15分钟)开盘,高,低,收盘价and volume“,” 2。Symbol“:” AAME“,” 3。Last Refreshed“:” 2019-11-18 16:00:00“,” 4。Interval“:” 15min“,” 5。Output Size“ :“紧凑”,“ 6。时区”:“美国/东部”}
2019-12-12 15:21:09.158 19798-19798 / com.animusabhi.dynamicjsonparsing E / JSON值{“ 2019-11-18 16:00:00”:{“ 1。open”: “ 1.6700”,“ 2。高”:“ 1.6700”,“ 3。低”:“ 1.5700”,“ 4。收盘”:“ 1.5700”,“ 5。交易量”:“ 1521”},“ 2019-11-11 18 15:45:00“:{” 1。打开“:” 1.6600“,” 2。高“:” 1.7400“,” 3。低“:” 1.6600“,” 4。关闭“:” 1.7400“,” 5.卷“:” 355“}}
检查一下,让我知道它是否无效。