有时候我们只想替换JSONObject某个对象的值,不想把所有对象的值都列出来。那就用for循环把所有的值重新赋值一遍。再单独给需要赋值的对象重新赋值
JSONObject itemObject = new JSONObject(sJson);
String labelData = itemObject.getString("TEMPLATE_DATA");
JSONObject labelDetailObject = new JSONObject(labelData);
for (Iterator<String> it = labelDetailObject.keys(); it.hasNext(); ) {
String key = it.next();
labelDetailObject.put(key, labelDetailObject.get(key));
}
labelDetailObject.put("LabelSizeWidth",labelWidth);
labelDetailObject.put("LabelSizeHeight",labelHeight);
labelDetailObject.put("LabelGap",labelGap);
for (Iterator<String> it = itemObject.keys(); it.hasNext(); ) {
String key = it.next();
itemObject.put(key, itemObject.get(key));
}
itemObject.put("TEMPLATE_DATA",labelDetailObject);
我的JSON格式是
{
"xx": "TEST001",
"xx": "19022001",
"TEMPLATE_DATA": {
"xx": "TEST001",
"xxx": "60",
"xx": "40",
"xx": "2",
"xx": "3",
"xx": [{
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy",
"xx": "yy"
}
}
}