JAVA这种JSON怎么生成,后面的值是一个数组,帮我看看谢谢

{
key:[
{"city":"北京"},{"location":["海淀","昌平","朝阳"]}
]
index:"BJ"
}

使用 http://www.json.org/java/index.html 上面的 Java 类。

package com.clark.app;

import org.json.JSONArray;
import org.json.JSONObject;

public class QuestionMain {
public static void main(String[] args) {
JSONObject object = new JSONObject();

JSONArray array = new JSONArray();
JSONObject city = new JSONObject();
city.put("city", "北京");
array.put(city);
JSONObject locationObj = new JSONObject();
JSONArray locationArr = new JSONArray();
locationArr.put("海淀");
locationArr.put("昌平");
locationArr.put("朝阳");
locationObj.put("location", locationArr);
array.put(locationObj);

object.put("key", array);
object.put("index", "BJ");

System.out.println(object);
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-10-09
MAP 1
key = city
value = 北京

MAP 2
key location
value = {[海淀],[昌平][朝阳]}

mapall = <k,y>的(MAP1,MAP2)

put key="BJ" value =mapall追问

东西是我在数据库里面调的不是手动生成的

追答

你选的答案还不是跟这一样的后台JAVA拼

第2个回答  2013-10-09
你是用js去拼还是java后台去拼?后者可以使用gson等工具包拼接一个结构生成追问

后台拼发送给前台怎么弄啊

追答

后台拼接好后其实就是String字符串,如果是jquery接的话可以使用java.io.Writer等传过去,如果是jsp等可以直接setAttribute

相似回答