获取jsonarray键名键名、jsonarray

2023-09-07 17:42:11 作者:꧁暴走的兔子꧂

我需要得到JsonArray的键名,因此JSON看起来是这样,请不,JSON是开始阵列支架,并在它里面有对象,这是做我猜是因为后端将有能力添加对象。

I need to get a key name of JsonArray, so JSON looks like this, please not, that JSON is starting with array brackets, and inside it it has objects, that was made i guess because back end will have the ability to add objects.

[
  {
    "tehnology": [ ]
  },
  {
    "science": []
  }
]

所以我需要从它技术和科学获得的名称,因为JSON可以动态改变,我怎么能实现呢?

So i need to get the names from it "technology" and "science", because json can dynamically change, how can I implement it?

推荐答案

JSONArray 包含的JSONObject 秒。检索每个的JSONObject 和使用键()来访问每个定义的关键的JSONObject

The JSONArray contains JSONObjects. Retrieve every JSONObject and use keys() to access the key defined in every JSONObject

 JSONArray jArray = new JSONArray(response);       
 for (int i = 0; i < jArray.length(); i++) {
      JSONObject object = jArray.optJSONObject(i);
      Iterator<String> iterator = object.keys();
      while(iterator.hasNext()) {
        String currentKey = iterator.next();
      }
 }