1

I have a json object in javascript,

filters = Object {assetType: "CAR~LIGHT TRUCK", bodyType: "SEDAN~SPORT UTIL"}

I need to pass these values along with 5 more string value in query string.

url = '/starHome/exportToCsv/'+tier+'/?period='+period+'&level2='+level2+'&level3='+level3+'&level4='+level4+'&filters='+filters;

window.location.href = url; when I tried to get filter parameter value in controller request.getparamter("filters"); I get "[object object]"

how can I pass this value to controller?. I have a pojo class contains all these fields. Can I make use of that class?.

5 Answers 5

8

I found the following as the best solution so far using Javasript

var a = {a:'foo',b:[1,2,3],g: {q:"8798"}};

var b = window.btoa(JSON.stringify(a)); 

console.log(b) // output eyJhIjoiZm9vIiwiYiI6WzEsMiwzXSwiZyI6eyJxIjoiODc5OCJ9fQ

// restore b again 
console.log(JSON.parse(window.atob(b))); // output : {a:'foo',b:[1,2,3],g: {q:"8798"}}
Sign up to request clarification or add additional context in comments.

2 Comments

you are restoring b in java script itself. My question was to get them in back end controller.
Oops , I missed this one , but in general still the same principle you can decode the base64 string in java then parse it as JSONObject
4

You can encode your json string in base64 and then use it in the url:

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/btoa

3 Comments

{window.btoa(filters);} is not helping.
@gauti what do you mean with is not helping?
I'm not able to decode the value as Map in controller.
1

You might be able to use jQuery.param()

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript">
    var filters = {assetType: "CAR~LIGHT TRUCK", bodyType: "SEDAN~SPORT UTIL"};
    filters = jQuery.param( filters );
    console.log("filters ", filters );
    //filters  assetType=CAR~LIGHT+TRUCK&bodyType=SEDAN~SPORT+UTIL

    url = '/starHome/exportToCsv/'+tier+'/?period='+period+'&level2='+level2+'&level3='+level3+'&level4='+level4+'&filters='+filters;
    </script>

1 Comment

jQuery.param() is fine, but is there a way that I can get that value into a Map<String,Object> in controller?
1

Another alternative is to use the encodeURI and decodeURI functions. First, we encode the JSON string so that it won't cause any issues in the URL. When we get the value back, we decode the string and then parse its JSON.

In this approach, the content is human readable and parsable at the backend without too much effort.

const values = {prices:[1,2,3], sale: true, minAmount: 500 };
const json = encodeURI(JSON.stringify(values));
const result = JSON.parse(decodeURI(json));

Comments

-1

Thanks Everyone, I have used JSON.stringify(filters); and in controller

String filtersString = request.getParameter("filters");
Map<String,Object> result = new ObjectMapper().readValue(filtersString, HashMap.class);

Kindly reply back, if there s a better approach.

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.