从java中的json中拆分所有数组元素

时间:2017-04-20 12:54:32

标签: java arrays json

如何在Java中分割JSON中的所有数组元素

{
  "id": "405844",
  "username": "abc8",
  "password_hash": "dkjfhs7rjkds932dajk2900932",
  "role": "customer",
  "followed_stores" : 
   [
     {
       "id": "sda",
       "created": {
                "date": "2016-06-28 14: 43 : 28",
                "epoch": 1467125008563
       }
     }
  ],
  "followed_influencers": [
    {
      "id": "sda",
      "created": {
               "date": " 2016-06-28 14: 43 : 28",
               "epoch": 1467125008563
      }
    }
  ]
}

我希望2 json中有3个JSON我必须有followed_stores数组,followed_influencer数组,并且在最后剩下的json中

1 个答案:

答案 0 :(得分:0)

您可以使用服务器响应创建org.json.JSONArray的实例:

String serverResponse = "YOUR STRING";
JSONArray serverJsonArray = new JSONArray(serverResponse);

然后填写产品清单:

ArrayList<String> products = new ArrayList<>(serverJsonArray.length());
for(int i = 0; i < serverJsonArray.length(); i++){
     products.add(serverJsonArray.getString(i));
}

或者如果你绝对想要一个String数组:

String[] products = new String[serverJsonArray.length()];
for(int i = 0; i < serverJsonArray.length(); i++){
     products[i] = serverJsonArray.getString(i);
}