jquery passes a json object to the java background

前台 

var data={}; 
data.fid="001"; 
data.fname="ok"; 
var myData=$.toJSON(data); 



alert(myData); 
打印的结果是:{"fid":"001","fname":"ok"} 

然后使用jquery的ajax  
$.ajax({type:"post", url:"uWkTm.do?operate=insertPlan", 
data:myData, 



后台 

BufferedReader br = new BufferedReader(new InputStreamReader( 
                    (ServletInputStream) request.getInputStream())); 
            String line = null; 
            StringBuilder sb = new StringBuilder(); 
            while ((line = br.readLine()) != null) { 
                sb.append(line); 
            } 
            JSONObject jsonObj = JSONObject.fromObject(sb.toString()); 
            JSONObject objs = jsonObj.getJSONObject("data");//Get the value according to the key in the json format (it is an object)  
            UwkTmMain mainitem = (UwkTmMain) JSONObject.toBean(objs,  
                    UwkTmMain.class);//Force converted to java object 

 

 

 

How to pass multiple json objects into the java background in the foreground

 

 

 

How to transfer data in json format in the foreground to the background

 

1. Assemble the data to be passed into the background into a string in JSON format:

 

var jsonStr = [{'name':'jim' , 'age':20} , {'name':'king' , 'age':26},{'name':'jge' , 'age':30}]

 

 

 

2. Use JQuery's ajax request background

 

 

 

jQuery.ajax({

 

type: "post",

 

url: url,

 

dataType : 'json',

 

data : {'mydata':jsonStr},

 

success: function(data,textStatus){

 

alert("Operation succeeded");

 

},

 

 error: function(xhr,status,errMsg){

 

        alert("Operation failed!");

 

            }

 

});

 

 

 

3. Reception and analysis of background data:

 

 

 

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. The operation is completed, the attachment is: JSONObject package;

 

chat-0.1.jar 

 

 

Conversion of Json object to Json string, conversion of JSON string to Java object

 

1. Conversion of Json object and Json string

1. Conversion methods supported by the jQuery plugin :

  $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr), can convert json string into json object

 

2. Conversion methods supported by browsers (Firefox, chrome, opera, safari, ie9, ie8) and other browsers:

  JSON.stringify(obj) converts JSON to string. JSON.parse(string) converts the string to JSON format;

var a={"name":"tom","sex":"男","age":"24"}; 
var b='{"name":"Mike","sex":"女","age":"29"}'; 
var aToStr=JSON.stringify(a); 
var bToObj=JSON.parse(b); 
alert(typeof(aToStr));  //string 
alert(typeof(bToObj));//object

 

3. Conversion methods supported by Javascript
eval('(' + jsonstr + ')'); //You can convert a json string into a json object, note that you need to wrap a pair of parentheses around the json character 
Note: ie8 (compatibility mode ), ie7 and ie6 can also use eval() to convert strings to JSON objects, but these methods are not recommended. This method is unsafe and eval will execute expressions in JSON strings.

4. The official JSON conversion method
http://www.json.org/ provides a json.js, so that ie8 (compatibility mode), ie7 and ie6 can support JSON objects and their stringify() and parse() methods ; 
You can get this js on https://github.com/douglascrockford/JSON-js, generally json2.js is used now.

 

Second, JSON string and java object conversion

1. Convert a list of java objects to an array of json objects and convert them to strings

    JSONArray array = JSONArray.fromObject(list);
    String jsonstr = array.toString();

 

2. Convert java objects to json objects and convert them to strings

  JSONObject object = JSONObject.fromObject(user);
  Log4jInit.ysulogger.debug(object.toString());

 

3. Convert JSON string to JAVA object array
  JSONArray json = JSONArray.fromObject(userStr);//userStr is json string
  List<User> users= (List<User>)JSONArray.toCollection(json, User.class) ;


4. Convert JSON string to JAVA object

 

  JSONObject jsonobject = JSONObject.fromObject(jsonStr);
  User user= (User)JSONObject.toBean(object,User.class);

 

 

 

前台 

var data={}; 
data.fid="001"; 
data.fname="ok"; 
var myData=$.toJSON(data); 



alert(myData); 
打印的结果是:{"fid":"001","fname":"ok"} 

然后使用jquery的ajax  
$.ajax({type:"post", url:"uWkTm.do?operate=insertPlan", 
data:myData, 



后台 

BufferedReader br = new BufferedReader(new InputStreamReader( 
                    (ServletInputStream) request.getInputStream())); 
            String line = null; 
            StringBuilder sb = new StringBuilder(); 
            while ((line = br.readLine()) != null) { 
                sb.append(line); 
            } 
            JSONObject jsonObj = JSONObject.fromObject(sb.toString()); 
            JSONObject objs = jsonObj.getJSONObject("data");//Get the value according to the key in the json format (is an object)  
            UwkTmMain mainitem = (UwkTmMain) JSONObject.toBean(objs,  
                    UwkTmMain.class);//Force converted to java object       How to pass multiple json objects into the java background in the foreground

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326464237&siteId=291194637