多层JSON数据,java后端接收json数据台如何接收

博客分类:
前台json格式的数据如何传入后台
1. 将要传入后台的数据组装成JSON格式的字符串:
var jsonStr = [{'name':'jim' , 'age':20} , {'name':'king' , 'age':26},{'name':'jge' , 'age':30}]
2. 使用JQuery的ajax请求后台
jQuery.ajax({
type: "post",
dataType : 'json',
data : {'mydata':jsonStr},
success: function(data,textStatus){
alert("操作成功");
error: function(xhr,status,errMsg){
alert("操作失败!");
3.后台数据的接收与解析:
String jsonStr = request.getParameter("jsonStr");
JSONArray jsonArray =
new JSONArray(jsonStr );
for(int i=0;i&jsonArray.length(); i++){
JSONObject jsonJ = jsonArray.getJSONObject(i);
jsonJ.getInt("name");
jsonJ.getString("age");
4. 操作完成, 附件为:JSONObject包;
(189.7 KB)
下载次数: 206
浏览 15526
不能用 运行报错 你试过没啊? 这是我之前开发过程中总结出来的 , 而且当时那个项目就是用这种方式传递多个json对象的. .不知道你报的是什么错.
WangQingHua123
浏览: 123187 次
来自: 深圳
你这是哪个版本的smack?
跟着思路走,可以写出来!非常感谢!
String jsonStr = request.getPar ...
下载Android SDK时出现Site Authentica ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据
1、DTO:Data Transfer Object,数据传送对象
2、对于日期格式的问题,也已经处理
3、json-lib-2.2.2-jdk13.jar (2.1在日期数组 json-&java有问题)
工具类JsonUtil代码如下:
public class JsonUtil {
/**页面传至后台时,json数据在request的参数名称*/
public final static String JSON_ATTRIBUTE = "json";
public final static String JSON_ATTRIBUTE1 = "json1";
public final static String JSON_ATTRIBUTE2 = "json2";
public final static String JSON_ATTRIBUTE3 = "json3";
public final static String JSON_ATTRIBUTE4 = "json4";
* 从一个JSON 对象字符格式中得到一个java对象,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
* @param object
* @param clazz
public static Object getDTO(String jsonString, Class clazz){
JSONObject jsonObject =
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
return JSONObject.toBean(jsonObject, clazz);
* 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
* beansList:[{}, {}, ...]}
* @param jsonString
* @param clazz
* @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
public static Object getDTO(String jsonString, Class clazz, Map map){
JSONObject jsonObject =
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
return JSONObject.toBean(jsonObject, clazz, map);
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
public static Object[] getDTOArray(String jsonString, Class clazz){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for(int i = 0; i & array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz);
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
* @param map
public static Object[] getDTOArray(String jsonString, Class clazz, Map map){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for(int i = 0; i & array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz, map);
* 从一个JSON数组得到一个java对象集合
* @param object
* @param clazz
public static List getDTOList(String jsonString, Class clazz){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for(Iterator iter = array.iterator(); iter.hasNext();){
JSONObject jsonObject = (JSONObject)iter.next();
list.add(JSONObject.toBean(jsonObject, clazz));
* 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
* @param object
* @param clazz
* @param map 集合属性的类型
public static List getDTOList(String jsonString, Class clazz, Map map){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for(Iterator iter = array.iterator(); iter.hasNext();){
JSONObject jsonObject = (JSONObject)iter.next();
list.add(JSONObject.toBean(jsonObject, clazz, map));
* 从json HASH表达式中获取一个map,该map支持嵌套功能
* 形如:{"id" : "johncon", "name" : "小强"}
* 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
* @param object
public static Map getMapFromJson(String jsonString) {
setDataFormat2JAVA();
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Map map = new HashMap();
for(Iterator iter = jsonObject.keys(); iter.hasNext();){
String key = (String)iter.next();
map.put(key, jsonObject.get(key));
* 从json数组中得到相应java数组
* json形如:["123", "456"]
* @param jsonString
public static Object[] getObjectArrayFromJson(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return jsonArray.toArray();
* 把数据对象转换成json字符串
* DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
* 数组对象形如:[{}, {}, {}, ...]
* map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
* @param object
public static String getJSONString(Object object) throws Exception{
String jsonString =
//日期值处理器
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
if(object != null){
if(object instanceof Collection || object instanceof Object[]){
jsonString = JSONArray.fromObject(object, jsonConfig).toString();
jsonString = JSONObject.fromObject(object, jsonConfig).toString();
return jsonString == null ? "{}" : jsonS
private static void setDataFormat2JAVA(){
//设定日期转换格式
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}));
public static void main(String[] arg) throws Exception{
String s = "{status : 'success'}";
System.out.println(" object : " + JsonUtil.getJSONString(s));
public class JsonUtil {
/**页面传至后台时,json数据在request的参数名称*/
public final static String JSON_ATTRIBUTE = "json";
public final static String JSON_ATTRIBUTE1 = "json1";
public final static String JSON_ATTRIBUTE2 = "json2";
public final static String JSON_ATTRIBUTE3 = "json3";
public final static String JSON_ATTRIBUTE4 = "json4";
* 从一个JSON 对象字符格式中得到一个java对象,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...}}
* @param object
* @param clazz
public static Object getDTO(String jsonString, Class clazz){
JSONObject jsonObject =
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
return JSONObject.toBean(jsonObject, clazz);
* 从一个JSON 对象字符格式中得到一个java对象,其中beansList是一类的集合,形如:
* {"id" : idValue, "name" : nameValue, "aBean" : {"aBeanId" : aBeanIdValue, ...},
* beansList:[{}, {}, ...]}
* @param jsonString
* @param clazz
* @param map 集合属性的类型 (key : 集合属性名, value : 集合属性类型class) eg: ("beansList" : Bean.class)
public static Object getDTO(String jsonString, Class clazz, Map map){
JSONObject jsonObject =
setDataFormat2JAVA();
jsonObject = JSONObject.fromObject(jsonString);
}catch(Exception e){
e.printStackTrace();
return JSONObject.toBean(jsonObject, clazz, map);
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
public static Object[] getDTOArray(String jsonString, Class clazz){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for(int i = 0; i & array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz);
* 从一个JSON数组得到一个java对象数组,形如:
* [{"id" : idValue, "name" : nameValue}, {"id" : idValue, "name" : nameValue}, ...]
* @param object
* @param clazz
* @param map
public static Object[] getDTOArray(String jsonString, Class clazz, Map map){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
Object[] obj = new Object[array.size()];
for(int i = 0; i & array.size(); i++){
JSONObject jsonObject = array.getJSONObject(i);
obj[i] = JSONObject.toBean(jsonObject, clazz, map);
* 从一个JSON数组得到一个java对象集合
* @param object
* @param clazz
public static List getDTOList(String jsonString, Class clazz){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for(Iterator iter = array.iterator(); iter.hasNext();){
JSONObject jsonObject = (JSONObject)iter.next();
list.add(JSONObject.toBean(jsonObject, clazz));
* 从一个JSON数组得到一个java对象集合,其中对象中包含有集合属性
* @param object
* @param clazz
* @param map 集合属性的类型
public static List getDTOList(String jsonString, Class clazz, Map map){
setDataFormat2JAVA();
JSONArray array = JSONArray.fromObject(jsonString);
List list = new ArrayList();
for(Iterator iter = array.iterator(); iter.hasNext();){
JSONObject jsonObject = (JSONObject)iter.next();
list.add(JSONObject.toBean(jsonObject, clazz, map));
* 从json HASH表达式中获取一个map,该map支持嵌套功能
* 形如:{"id" : "johncon", "name" : "小强"}
* 注意commons-collections版本,必须包含org.apache.commons.collections.map.MultiKeyMap
* @param object
public static Map getMapFromJson(String jsonString) {
setDataFormat2JAVA();
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Map map = new HashMap();
for(Iterator iter = jsonObject.keys(); iter.hasNext();){
String key = (String)iter.next();
map.put(key, jsonObject.get(key));
* 从json数组中得到相应java数组
* json形如:["123", "456"]
* @param jsonString
public static Object[] getObjectArrayFromJson(String jsonString) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return jsonArray.toArray();
* 把数据对象转换成json字符串
* DTO对象形如:{"id" : idValue, "name" : nameValue, ...}
* 数组对象形如:[{}, {}, {}, ...]
* map对象形如:{key1 : {"id" : idValue, "name" : nameValue, ...}, key2 : {}, ...}
* @param object
public static String getJSONString(Object object) throws Exception{
String jsonString =
//日期值处理器
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
if(object != null){
if(object instanceof Collection || object instanceof Object[]){
jsonString = JSONArray.fromObject(object, jsonConfig).toString();
jsonString = JSONObject.fromObject(object, jsonConfig).toString();
return jsonString == null ? "{}" : jsonS
private static void setDataFormat2JAVA(){
//设定日期转换格式
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"}));
public static void main(String[] arg) throws Exception{
String s = "{status : 'success'}";
System.out.println(" object : " + JsonUtil.getJSONString(s));
对于java对象转换成json数据格式时,要对日期格式化常用格式,类:JsonDateValueProcessor
import java.text.SimpleDateF
import java.util.D
import net.sf.json.JsonC
import net.sf.json.processors.JsonValueP
* @author johncon
* 创建日期
* json日期值处理器
public class JsonDateValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd HH:mm:ss";
public JsonDateValueProcessor() {
public JsonDateValueProcessor(String format) {
this.format =
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
private Object process( Object value, JsonConfig jsonConfig ) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return value == null ? null : value.toString();
public String getFormat() {
public void setFormat(String format) {
this.format =
import java.text.SimpleDateF
import java.util.D
import net.sf.json.JsonC
import net.sf.json.processors.JsonValueP
* @author johncon
* 创建日期
* json日期值处理器
public class JsonDateValueProcessor implements JsonValueProcessor {
private String format = "yyyy-MM-dd HH:mm:ss";
public JsonDateValueProcessor() {
public JsonDateValueProcessor(String format) {
this.format =
public Object processArrayValue(Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
return process(value, jsonConfig);
private Object process( Object value, JsonConfig jsonConfig ) {
if (value instanceof Date) {
String str = new SimpleDateFormat(format).format((Date) value);
return value == null ? null : value.toString();
public String getFormat() {
public void setFormat(String format) {
this.format =
对于对象中有明确类型的对象属性,可不管;但对象中有集合属性的,由于类型不明确,所以要先明确类型:
String jsonString = request.getParameter("json");
//增加对象中的集合属性的类型以及对象元素中的对象属性的集合属性的类型
Map clazzMap = new HashMap();
//secondItems是FirstDTO里的集合属性
clazzMap.put("secondItems", SecondDTO.class);
//thirdItems是SecondDTO里的集合属性
clazzMap.put("thirdItems", ThirdDTO.class);
FirstDTO firstDTO = (FirstDTO)JsonUtil.getDTO(jsonString, FirstDTO.class, clazzMap);
浏览: 42394 次
来自: 深圳
适用,方法好~!
写的很不错 最后的效果做的很好
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'每天不是进步就是退步
JAVA后台接收前台传过来的json字符串并解析获得key 和value
前台代码:
type:"post",
url:"project/updateProject",
formdata: JSON.stringify(formdata),
tabname:$("#tabname").val(),
id: $("#proid").val()
success:function(data){
alert("保存成功");
error:function(data){
alert("网络错误,保存失败");
});后台代码:
//更新项目信息
public void updateProject(){
String formdata = getPara("formdata");
JSONObject jo = JSONObject.fromObject(formdata);
//将json字符串转成json对象后遍历键值对
Map&String, Object& map =
Record r = new Record();
for (Entry&String, Object& entry : map.entrySet()) {
r.set(entry.getKey(), entry.getValue());
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!&>&java后台生成JSON数据
java后台生成JSON数据
上传大小:1.68MB
java后台生成JSON数据和EXT互传数据,全靠它实现。
综合评分:4
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var parentWrap = $(this).parents(".respond_box"),
q = parentWrap.find(".form1").serializeArray(),
resStr = $.trim(parentWrap.find(".res_area_r").val());
console.log(q);
//var res_area_r = $.trim($(".res_area_r").val());
if (resStr == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
//var mess = $(".res_area_r").val();
var mess = resS
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, data.com_username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click", '.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
var parentWrap = $(v).parents(".respond_box");
parentWrap.find(".res_area_r").val($.trim(parentWrap.find(".res_area").val()));
评论共有25条
正需要呢~还是这里的资源齐全:)
可以使用,学习了哦
还是可以的,有待改进
VIP会员动态
热门资源标签
CSDN下载频道资源及相关规则调整公告V11.10
下载频道用户反馈专区
下载频道积分规则调整V1710.18
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
资源所需积分/C币
当前拥有积分
当前拥有C币
输入下载码
为了良好体验,不建议使用迅雷下载
java后台生成JSON数据
会员到期时间:
剩余下载个数:
剩余积分:0
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
无法举报自己的资源
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可返还被扣除的积分
被举报人:
zyfcomputer
举报的资源分:
请选择类型
资源无法下载 ( 404页面、下载失败、资源本身问题)
资源无法使用 (文件损坏、内容缺失、题文不符)
侵犯版权资源 (侵犯公司或个人版权)
虚假资源 (恶意欺诈、刷分资源)
含色情、危害国家安全内容
含广告、木马病毒资源
*详细原因:
java后台生成JSON数据json的处理java后台和前台之间
js通过ajax传给后台一个json数组字符串:
[{'prjcode':'A00','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'},{'prjcode':'A03','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'},{'prjcode':'B00','countyname':'大悟县','pro_1':'A','pro_2':'A','pro_3':'A','pro_4':'A','pro_5':'A','pro_6':'A','pro_7':'A','pro_8':'A','pro_9':'A','pro_10':'A','pro_11':'A','pro_12':'A','pro_13':'A','pro_14':'A','pro_15':'A'}]
后台接受进行遍历存储
String jsonstr = request.getParameter(&jsonstr&);
&&&&&&&&&&& JSONArray json = JSONArray.fromObject(jsonstr);
&&&&&&&&&&& Object[] obj=json.toArray();
&&&&&&&&&&& for(int i=0;i&obj.i++){&
&&&&&&&&&&&&& JSONObject object = JSONObject.fromObject(obj[i]);
&&&&&&&&&&&&& String prjcode=object.get(&prjcode&).toString();
&&&&&&&&&&&&& String countyname=object.getString(&countyname&).toString();
&&&&&&&&&&&&& String pro_1=object.getString(&pro_1&).toString();
&&&&&&&&&&&&& String pro_2=object.getString(&pro_2&).toString();
&&&&&&&&&&&&& String pro_3=object.getString(&pro_3&).toString();
&&&&&&&&&&&&& String pro_4=object.getString(&pro_4&).toString();
&&&&&&&&&&&&& String pro_5=object.getString(&pro_5&).toString();
&&&&&&&&&&&&& String pro_6=object.getString(&pro_6&).toString();
&&&&&&&&&&&&& String pro_7=object.getString(&pro_7&).toString();
&&&&&&&&&&&&& String pro_8=object.getString(&pro_8&).toString();
&&&&&&&&&&&&& String pro_9=object.getString(&pro_9&).toString();
&&&&&&&&&&&&& String pro_10=object.getString(&pro_10&).toString();
&&&&&&&&&&&&& String pro_11=object.getString(&pro_11&).toString();
&&&&&&&&&&&&& String pro_12=object.getString(&pro_12&).toString();
&&&&&&&&&&&&& String pro_13=object.getString(&pro_13&).toString();
&&&&&&&&&&&&& String pro_14=object.getString(&pro_14&).toString();
&&&&&&&&&&&&& String pro_15=object.getString(&pro_15&).toString();
&&&&&&&&&&& }&

我要回帖

更多关于 java后端接收json数据 的文章

 

随机推荐