com.google.gson.JsonObject不能转换为com.google.gson.JsonArray在Java中

jackt1997:

我想放置“速度”到JsonArray使用GSON,但它不工作,有人可以告诉我如何此JSON进入一个地图,或在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
  }
}

编辑:添加的代码

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"); 
阿布巴卡尔·汗:

你会得到这条线在这里的例外:

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

因为你可以在你输入文件中看到是一个JSON对象不是一个JSON阵列。让我告诉你在语法上的区别:

1.价格为JSON对象(如在你的情况下):

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

2.价格作为JSON阵列:

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

在第二种情况,你可以把它作为JSON的JSON数组对象!我建议您阅读的数据类型JSON W3教程

好了现在解决您的输入作为提取所有的东西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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

用于JSON解析的Android模型显示ClassCastException:com.google.gson.JsonObject无法转换为com.google.gson.JsonArray

com.google.gson.JsonPrimitive无法转换为com.google.gson.JsonObject错误

如何在Java中解析Json文件时无法转换com.google.gson.JsonArray

获取java.lang.NoClassDefFoundError:com / google / gson / Gson

Appium:java.lang.NoSuchMethodError:com.google.gson.GsonBuilder.setLenient()Lcom / google / gson / GsonBuilder;

java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;

无法解决:com.google.code.gson:gson.2.2.2

无法解决:com.google.code.gson:gson:2.3.1

com.google.gson.internal.LinkedTreeMap 类不能转换为类 Return

如何将我的JsonObject(com.google.gson.JsonObject)转换为GenericRecord(org.apache.avro.generic.GenericRecord)类型

java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为asd.Grade

使用com.google.gson解析JSON

获取java.lang.ClassNotFoundException:com.google.gson.Gson用于Maven项目

如何在Android Studio中修复“无法解决:com.google.gson:gson:2.8.5”

JSONObject(org.json)到JsonElement(com.google.gson)

无法加载类“ com.google.gson.JsonObject”

造成原因:com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:预期第':'行

Google Gson-com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为字符串,但为BEGIN_OBJECT

java.lang.NoSuchMethodError:com.google.gson.GsonBuilder.setLenient()Lcom / google / gson / GsonBuilder; 通过ChromeDriver启动Chrome时

如何在Java中使用com.google.gson.JsonObject将值设置为null?

我收到“ java.lang.ClassNotFoundException:com.google.gson.Gson”错误,即使它是在我的类路径中定义的

改造2中的'com.google.code.gson:gson:2.6.1'依赖项在拥有自己的gson转换器时起什么作用?

java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为java.util.LinkedHashMap

线程“main”中的异常 java.lang.NoClassDefFoundError: com/google/gson/JsonParseExceptiong

com.google.gson.internal.LinkedHashTreeMap无法转换为我的对象

com.google.gson.internal.LinkedTreeMap类无法转换为Partner类

com.google.gson.internal.LinkedTreeMap无法转换为我的自定义类

Gradle Android Studio 2.3:无法解析'com.google.code.gson:gson:2.8.0'

无法解析com.google.code.gson:gson:2.8.2。由于防火墙