[{"period":"","billing_mode":"checked","price":"1500","bundleid":0,"name":"hello stack","deviceid":"923is3j4","date":"2016-06-23","userid":0,"type":"mobile","strutid":999}]
我有这个JSON数组,里面有JSON对象。 我想要
的JSON对象"period":""
要删除。有可能吗?请帮我谢谢。 P.S:这是针对Java而不是Javascript。
答案 0 :(得分:1)
看你能用这种方式做什么
这适用于PHP家伙:)
$json='{"period":"","billing_mode":"checked","price":"1500","packageid":0,"name":"hello stack","subscriberid":"9283is3j4","date":"2016-06-23","serviceid":0,"type":"event","programid":999}';
$arr = json_decode($json, true);
unset($arr['period']);
echo json_encode($arr);
答案 1 :(得分:0)
如果您想要java
,然后使用java-json.jar
,您可以按以下方式实现:
public static void main(String[] args) throws JSONException {
String s="{\"period\":\"\",\"billing_mode\":\"checked\",\"price\":\"1500\",\"packageid\":0,\"name\":\"hello stack\",\"subscriberid\":\"9283is3j4\",\"date\":\"2016-06-23\",\"serviceid\":0,\"type\":\"event\",\"programid\":999}";
JSONObject jsonObj = new JSONObject(s);
JSONArray nameArray = jsonObj.names();
List<String> keyList = new ArrayList<String>();
for (int i = 0; i < nameArray.length(); i++) {
keyList.add(nameArray.get(i).toString());
}
for (String key : keyList) {
if (jsonObj.get(key).equals(""))
{
jsonObj.remove(key);
}
}
System.out.println(jsonObj.toString());
}
如果我们在迭代JSON Key
时删除了JSONArray
,那么它会给出以下异常:
Exception in thread "main" java.util.ConcurrentModificationException
所以我通过将JSON array
复制到Java collection
并使用JSONObject.names()
方法获取JSONArray of keys.