String json_string_from_server = "{\"test1\":\"test1_value\",\"test2\":\"test2_value\"}";
如何循环JSON并打印所有键和值。
答案 0 :(得分:1)
假设jsonString是一个变量,你从php中获取所有json。您需要按如下方式迭代数组:
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)" class="relative" style="position:relative">
<div id="TextBoxesGroup">
<div id="TextBoxDiv1">
<label id="label1">Text Here</label>
<input id="textbox1" class="clickedit" type="text" class="draggable" placeholder="Text Here" />
<div class="clearfix" id="hey"></div>
</div>
</div>
</div>
<button id="save">Save</button>
答案 1 :(得分:1)
您可以简单地解析Json字符串,如下所示 -
String json_string_from_server = "{\"test1\":\"test1_value\",\"test2\":\"test2_value\"}";
JSONObject jObj = new JSONObject(json_string_from_server);
String val_Test1 = jObj.getString("test1");
String val_Test2 = jObj.getString("test2");
案例2:
String json_string_from_server = "{
"result" : [
{\"test1\":\"test1_values_baru\", \"test2\":\"test2_values\"},
{\"test1\":\"test1_values\", \"test2\":\"test2_values\"}
]
}";
JSONObject jObj = new JSONObject(json_string_from_server);
JSONArray jResultArray = jObj.getJSONArray("result");
for(int i=0; i<jResultArray.length(); i++){
JSONObject jResultObj = jResultArray.getJSONObject(i);
String val_Test1 = jResultObj.getString("test1");
String val_Test2 = jResultObj.getString("test2");
}
答案 2 :(得分:0)
String json_string="[{\"test1\":\"test1_values_baru\",\"test2\":\"test2_values\"},{\"test1\":\"test1_values\",\"test2\":\"test2_values\"}]";
JSONArray jResultArray = new JSONArray(json_string);
for(int i=0; i<jResultArray.length(); i++){
JSONObject jResultObj = jResultArray.getJSONObject(i);
String val_Test1 = jResultObj.getString("test1");
String val_Test2 = jResultObj.getString("test2");
}