简体   繁体   English

com.google.gson.JsonObject无法强制转换为Java中的com.google.gson.JsonArray

[英]com.google.gson.JsonObject cannot be cast to com.google.gson.JsonArray in Java

I'm trying to place the "rates" into a JsonArray using GSON, but it doesn't work, can somebody advise how to get this JSon into a Map, or an array or something of the sort using Gson in Java? 我正在尝试使用GSON将“率”放入JsonArray中,但是它不起作用,有人可以建议如何将这个JSon放入Map中,或者使用Java中的Gson将数组或类似的东西?

{
  "success": true,
  "timestamp": 1548277447,
  "base": "USD",
  "date": "2019-01-23",
  "rates": {
    "AED": 3.673021,
    "ARS": 37.537301,
    "AUD": 1.400099,
    "BGN": 1.717902,
    "BRL": 3.7657,
    "BWP": 10.52802,
    "CAD": 1.334645,
    "CHF": 0.994703,
    "CLP": 671.898015,
    "CNY": 6.791896,
    "COP": 3151.5,
    "DKK": 6.559594,
    "EGP": 17.891044,
    "EUR": 0.878542,
    "GBP": 0.76529,
    "HKD": 7.84525,
    "HRK": 6.530898,
    "HUF": 279.43017,
    "ILS": 3.673794,
    "INR": 71.13502,
    "ISK": 120.350185,
    "JPY": 109.595496,
    "KRW": 1126.589831,
    "KZT": 378.239562,
    "LKR": 182.190238,
    "LTL": 2.95274,
    "LVL": 0.60489,
    "LYD": 1.390468,
    "MXN": 19.0361,
    "MYR": 4.13696,
    "NOK": 8.56596
  }
}

Edit: Added code 编辑:添加代码

HttpURLConnection fixerConnection = (HttpURLConnection) url.openConnection();
          fixerConnection.setRequestMethod("GET");
          BufferedReader jsonData = new BufferedReader(new InputStreamReader(fixerConnection.getInputStream()));  
          JsonObject allData = new JsonParser().parse(jsonData).getAsJsonObject();           
          JsonArray jArray =  allData.getAsJsonArray("rates");//getAsJsonObject("symbol"); 

You will get an Exception for this line here: 您将在此处获得此行的例外情况:

JsonArray jArray =  allData.getAsJsonArray("rates");

because as you can see in your input file rates is a JSON Object not an JSON array. 因为你可以在你输入文件速率看到的是一个JSON对象不是一个JSON阵列。 Let me show you the difference in syntax : 让我告诉你语法上的区别:

1. Rates as a JSON Object(As in your case): 1.作为JSON对象的速率(与您的情况一样):

"rates": { "AED": 3.673021, "ARS": 37.537301}

2. Rates as a JSON Array: 2.作为JSON数组的速率:

"rates": [ {"AED": 3.673021}, {"ARS": 37.537301}]

In the 2nd case you can get it as JSON Array of JSON Objects! 在第二种情况下,您可以将其作为JSON对象的JSON数组获得! I suggest reading the DataTypes of JSON W3 Tutorial 我建议阅读JSON W3教程的DataTypes

Okay now the Solution to extracting all that stuff in your input as JSON Object 好的,现在解决方案是将输入中的所有内容作为JSON对象提取

HttpURLConnection fixerConnection = (HttpURLConnection) url.openConnection();
          fixerConnection.setRequestMethod("GET");
          BufferedReader jsonData = new BufferedReader(new 
          InputStreamReader(fixerConnection.getInputStream()));  
          JsonObject allData = new JsonParser().parse(jsonData).getAsJsonObject();
          // Now Take Rates as JSON Object and capture it in a Map.
          JsonObject rates =  allData.getAsJsonObject("rates");
          Set<Map.Entry<String, JsonElement>>  entries = rates.entrySet();

暂无
暂无

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

相关问题 用于JSON解析的Android模型显示ClassCastException:com.google.gson.JsonObject无法转换为com.google.gson.JsonArray - Android Model for JSON parsing shows ClassCastException: com.google.gson.JsonObject can not be casted to com.google.gson.JsonArray com.google.gson.JsonPrimitive无法强制转换为com.google.gson.JsonObject - com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject Lotus Notes Java代理的GSON库错误-java.lang.NoClassDefFoundError:com.google.gson.JsonObject - GSON library error with Lotus Notes Java Agent - java.lang.NoClassDefFoundError: com.google.gson.JsonObject 如何添加com.google.gson.JsonArray的特定索引? - How to add in a specific index of com.google.gson.JsonArray? 无法加载“com.google.gson.JsonObject”类 - Unable to load class 'com.google.gson.JsonObject' 你如何在java中使用com.google.gson.JsonObject将值设置为null? - How do you set a value to null with com.google.gson.JsonObject in java? 将com.google.gson.JsonObject转换为org.json.simple.JSONObject - Convert com.google.gson.JsonObject to org.json.simple.JSONObject 找不到类型为com.google.gson.JsonArray的返回值的转换器 - No converter found for return value of type: class com.google.gson.JsonArray java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.gson.internal.LinkedTreeMap - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.gson.internal.LinkedTreeMap java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法强制转换为com.example - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM