如何从json对象获取json数组? [英] How to get the json array from the json object?

查看:115
本文介绍了如何从json对象获取json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要您的帮助来从json对象读取随机命名的数组.

Hello every one I need your help for reading the random named array from the json object.

在此任务中,客户端根据其要求制作json对象.

In this task client make the json object according to his requirements.

喜欢

 {
    "tags":[ "demo 1","demo 2","demo 3","demo 4","demo 5","N" ] 
 }

我正在使用"N" 来定义一个数组中项目的数量不受限制.

I'm using "N" for defining the unlimited numbers of items in one array.

在此代码中,用户使用标签键将json数组放入json对象.

Here in This code user use tags key to put json Array in json object.

用户还可以将强调文本数组设置为客户

User can also make emphasized textarray as Clients

  {
        "Clients":[ "demo 1","demo 2","demo 3","demo 4","demo 5","N" ] 
  }

我知道如何解析

JSONArray jsonMainArr = new JSONArray(String.valueOf(ObjectName.getJSONArray("Keyname")));

现在我的问题是,如果我们不知道标签客户

Now my Question is How to get Json Array if we Don't Know the KeyName like tags or Clients

推荐答案

您可以执行以下操作:

JSONObject mainJsonObject = new JSONObject(jsonString);
Iterator<?> keys = mainJsonObject.keys();
if (keys.hasNext()){
    String key = (String) keys.next();
    JSONArray jsonMainArr = mainJsonObject.getJSONArray(key);
}

在上面的代码中,我从原始的jsonString获取主JsonObject,并使用迭代器获取对象内部的第一个键,并使用它来获取JsonArray.

In the above code I am getting the main JsonObject from the original jsonString and using the iterator getting the first key inside the object and using it to fetch the JsonArray.

尝试此解决方案,让我知道您在实施该解决方案时是否遇到任何问题.

Try this solution and let me know if you have any issue implementing the same.

这篇关于如何从json对象获取json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆