我有一个JSON对象,看起来像这样:
cb = {"content":[{"name":"abc"}{"name":"bcd"}{"name":"xyz"}...]}
我将JSON库导入为:
import org.json.JSONObject;
现在我想使用for循环来检索每个子对象中的name值...
for(int x = 10; x < numberOfSubElemtnts; x = x+1) {
//print out the name values
}
输出应为:
ABC
BCD
XYZ
我应该如何评估数组的长度并打印出名称值?
答案 0 :(得分:3)
您可以像这样迭代数组:
String content = "{\"content\":[{\"name\":\"abc\"},{\"name\":\"bcd\"},{\"name\":\"xyz\"}]}";
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("content");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("name"));
}
答案 1 :(得分:1)
var checkBoxData = {};
$('.myCheck').on('ifChecked', function (event) {
var checkBoxIndex = $(this).index();
// This callback is triggered when checkbox is checked
$.ajax({
url: '/Home/getCategoryItems',
type: "GET",
cache: false,
data: {
name: $(this).attr("name")
},
success: function (data) {
checkBoxData[checkBoxIndex] = data;
updateCarsSection();
}
});
});
$('.myCheck').on('ifUnchecked', function (event) {
// This callback is triggered when checkbox is unchecked
checkBoxData[$(this).index()] = "";
updateCarsSection();
}
function updateCarsSection()
{
// Clear out the div
$("#displayCarsSection").empty();
var updatedData = "";
$.each(checkBoxData, function (key, value) {
updatedData += value;
});
// Append data based on currently selected checkboxes
$("#displayCarsSection").append(updatedData);
}
同样在这个例子中,您必须更改字符串的格式以适合我的格式