如何创建下面给出的json?
<?php
$cid= (string)$_GET['cid'];//I passed this from another page using get method
echo $cid; //My code works up to this point
$record = mysql_query("select * from questions where QType = '$cid'");
$array = array();
while($row = mysql_fetch_assoc($record))
{
$array[] = $row;
}
for($var = 0; $var<count($array);$var++)
{
echo $array[$var]['Question'].'<br>';
}
?>
答案 0 :(得分:2)
这取决于您的JSON库,但是对于org.json.simple或net.sf.json来说,就像这样:
JSONObject objectInArray = new JSONObject();
objectInArray.put("id", "some value");
objectInArray.put("name", "some value");
JSONArray jsonArray = new JSONArray();
jsonArray.add(objectInArray);
JSONObject data = new JSONObject();
data.put("data", jsonArray);
答案 1 :(得分:0)
使用Google的gson-2.x.jar 使像这样的实体类
public class MyEntity{
@SerializedName("data")
private List<Data> m_listData;
//Setter getters
public List<Data> getData() {
return m_listData;
}
public void setData(List<Data> data) {
this.m_listData= data;
}
}
class Data{
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id= id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
}
//and then convert you json string to java object
public class Test{
public static void main(String[] args){
String jsonString="your JSON string"
Gson obj_gson = new Gson();
MyEntity obj_myEntity=obj_gson.fromJson(jsonString, MyEntity.class);
}
}