0

I have created a rest service like this:

@GET
@Path("/getsummary")
@Produces("application/json")

public Response summary()
{

        VoltDAOImpl voltDao = new VoltDAOImpl();
        Map<String ,List<HashMap<String,String>>> returnList=       voltDao.getOrderDetails("PEAKM" , "Hydra" ,
                 "" ,  voltDao.client,"notional" ,1000);
        List<HashMap<String,String>> totalSlpSummaryList = returnList.get("total_slp_summary");
        List<HashMap<String,String>> totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side");
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

            String json1 = null;
            String json2 = null;
            String json3 = null;
            try {
                json1 = ow.writeValueAsString(totalSlpSummaryList);
                json2 = ow.writeValueAsString(totalSlpSummaryBySideList);
                json3="["+json1+","+json2+"]";
                //json3 = json1 + "\n" + json2;
                System.out.println(json1);
                System.out.println(json2);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    return Response.status(200).entity(json3).build();
}

And then I'm consuming this in a javascript like this:

var rootURL = "http://localhost:8181/RestServiceProject/rest/WebService/getsummary";
 function findBySummary() {
    console.log("inside loading grid");
    var returnval1;
     $.ajax({
         type: 'GET',
         url: rootURL,
         dataType: "json",
         async: false,
         success: function(json3){  

            returnval1 = json3;
            console.log(returnval1);

         }

     });
     return returnval1;
 }

console.log(returnval1); 

return the following result: returnval1

later I'm calling findBySummary(); and doing the following:

    var json3 = findBySummary();

        var json4 = json3[0];
console.log("json4");
        console.log(json4);
        var json_temp = JSON.stringify(json4);
        console.log("json_temp")
        console.log(json_temp);
        var json_temp1 = json_temp[0];
        console.log("json temp1");
        console.log(json_temp1);

Following is the log output for this: log output

I am trying to convert json_temp, which is, :

[{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}]

to this(json_temp1):

{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}

But why am I getting [ only for json_temp1??

1 Answer 1

3

Note that JSON.stringify(json4) returns a string.

So, the code json_temp[0] will return the first element in this string.

What you want is:

var json_temp1 = json4[0];

and not:

var json_temp1 = json_temp[0];

The code below is wrong for your need:

var json_temp = JSON.stringify(json4);
console.log("json_temp")
console.log(json_temp);
var json_temp1 = json_temp[0];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.