Java, JS (jQuery) and Ajax. Get a specific value of JSON.

Get the specific value of the data returned by JSON type from Java with jQuery (client side).

A memorandum of processing that handles JSON Uses Struts 1.3.10.

Ajax with JS. Call Java.

main.js


$.ajax({
    url     : "./SearchBook.do",
    type    : "POST",
    async   : true,
    data    : "Data to throw to Java here",
    dataType: "json",
    success : function(result) {
        //Processing on success
    }
});

Put the result of various processing with the data received from JS into JSON type on Java side. String type and int type are prepared as data.

main.java


String title = "spark"
int totalPage = 152;

JSONObject result = new JSONObject();
result.put("title", title);
result.put("number of pages", totalPage);

The JSON type below is returned. {"Title ":" Sparks "," Number of pages ":" 152 "}

$.ajax({
    --abridgement--
    success	: function(result) {
        //Processing on success
    }
});

All you have to do is retrieve the target value with the key.

main.js


success	: function(result) {
    //Processing on success
    $("#foo").text(result["title"]);
    $("#bar").text(result["number of pages"]);
}

For the time being, this is done. If there is a better way, please teach me.

There is also a different way of writing ...

Change the key to alphabetic characters in Java.

main.java


String title = "spark"
int totalPage = 152;

JSONObject result = new JSONObject();
result.put("bookTitle", title);
result.put("totalPage", totalPage);

On the JS side, connect with. (Dot) and specify the key to get the value.

main.js


$.ajax({
    url     : "./SearchBook.do",
    type    : "POST",
    async   : true,
    data    : "Data to throw to Java here",
    dataType: "json",
    success	: function(result) {
        //Processing on success
        $("#foo").text(result.bookTitle);
        $("#bar").text(result.pager.totalPage);
}

Whether to parse or not

in short...

main.js


$.ajax({
    url     : "./SearchBook.do",
    type    : "POST",
    async   : true,
    data    : "Data to throw to Java here",
    dataType: "json",
    success	: function(result) {
        //Processing on success
        $("#foo").text(result["title"]);
        $("#bar").text(result["number of pages"]);
    }
});

Or

main.js


$.ajax({
    url     : "./SearchBook.do",
    type    : "POST",
    async   : true,
    data    : "Data to throw to Java here",
    success	: function(result) {
        //Processing on success
        var data = JSON.parse(result);
        $("#foo").text(result["title"]);
        $("#bar").text(result["number of pages"]);
    }
});

To be.

Recommended Posts

Java, JS (jQuery) and Ajax. Get a specific value of JSON.
[Java] Get the day of the specific day of the week
[Java] How to get to the front of a specific string using the String class
[Java] Get a random value from an array
Get a list of MBean information for Java applications
[Java] How to get the maximum value of HashMap
Get a list of classes in a Guava specific package
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] Throw a request and display the screen ② (GET / POST)
[Java] Throw a request and display the screen (GET / POST)
Advantages and disadvantages of Java
Utilization of Java array elements, for, length, value, and extended for statements
[Java] Get the dates of the past Monday and Sunday in order
Get the public URL of a private Flickr file in Java
Sort a List of Java objects
Java pass by value and pass by reference
[Java] Judgment of identity and equivalence
A brief description of JAVA dependencies
Get stuck in a Java primer
Java HashMap Transform js json Formal
A collection of phrases that impresses the "different feeling" of Java and JavaScript
[Java] Get Json from URL and handle it with standard API (javax.script)
The story of forgetting to close a file in Java and failing
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
Get a rough idea of the differences between protocols, classes and structs!
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
Let's get rid of Devise's sickness and lead a comfortable Rails life!
How to create your own annotation in Java and get the value