Comprehending Java's HashMap in Javascript as a JSON object - javascript

I have an application where a Java HashMap is sent over via HTTP protocol, and I have to comprehend that HashMap object in Javascript before appending that JSON object to MySQL database.
I have tried multiple approaches such as
Converting HashMap to String in Java code and parsing that in Javascript with JSON library
Appending raw HashMap object to MySQL.
The first approach did not work because when Java converts HashMap object to String, it strips off all quotation marks, which gives Javascript a confusion.
The second approach simply does not work.
Is there anyone with this similar experience who can give me a seamless solution?
Thanks

Solved by sending Gson object.

Related

Creating Binary Data Structures in Javascript

I'm building a web app, and need to be able to encode user-generated data as concisely as possible before transmitting it to my server. In the past I've used Flash, and it had a very neat system where for any class that you want to serialize, you could write a pair of functions that would describe exactly how to serialize the data. For example:
out.writeShort(session);
out.writeUnsignedInt(itemID);
out.writeObject(arbitraryData);
out.writeShort(score);
You would have to write an equivalent function to read bytes from the serialized data and build the class from it.
Once data is serialized it could be encoded into a Base64 string for safe network transmission to the server.
I can't figure out how to do this in Javascript? JSON is nice and easy but it's incredibly wasteful, sending all object key/value pairs, and unless I'm mistaken everything is encoded as a string? So the value false is encoded as the string "false"?
Any advice on how to implement this in Javascript would be greatly appreciated! Use of libraries is fine so long as they work both on Node and in browser.
Look at this answer. You can use BSON format (Binary JSON) and it doesn't have those features of JSON you mentioned.

how to send the java output in json format to html forms

I had wsdl link and used in java program to get the data in json format and how can use the json to send to the html forms to the display the data what are the technologies I want to use for this process can anyone please suggest me if you can.
With json lib. You can check it on google.
And I dont know maybe you can use struts session.
Gson can be best bet in your case.
Gson is a Java library that can be used to convert Java Objects into
their JSON representation. It can also be used to convert a JSON
string to an equivalent Java object. Gson is an open-source project
hosted at http://code.google.com/p/google-gson.

GWT JavaScriptObject: get original json string?

I have been working with JSONP on my GWT application. When my server sends a json string, I can get it in the form of a JavaScriptObject on the client side.
My problem is my json has complicated structures: using maps, nests with a lot of different keys. That is a big pain to extract data (I may have to write few hundred functions for all keys to extract data one by one and some complicated codes to fill maps).
I am considering few solutions:
Encode and send whole json strings as normal strings to client (as a value of a simple json string). Just worry my encoded strings may be few time longer than the original ones and may easily exceed the limit of 2k long
Convert back a JavaScriptObject into a pure string (similar to one I sent from the server)
After having a pure string I will parse it using some json parsers / methods to the structures I feel convenient.
My questions:
1) How to convert back a JavaScriptObject object into a pure / original json string?
2) Any idea about solutions?
Many thanks
1) Convert JavaScriptObject to JSON: JsonUtils.stringify(yourJSO)
Convert JSON to JavaScriptObject: JsonUtils.safeEval(jsonString);
2) Did you think about using AutoBeans?? Check out the GWT page

Is there a way to create the exact same String of a Java <-> JSON mapped object like the JSON.stringify(jsObject) creates?

my problem is that i need the exact same String for hashing them later on. The first String comes from the javascript object with JSON.stringify, and the second one is a java object, mapped from the JSON.
I need a method to "use" the stringify method on my java object.
All quotes, spaces...need to be exact same because I need the same hashvalue to compare them later on. If someone here has a wink or the same problem, i'll be happy if you share it!! Thanks in advance
i can offer a couple of suggestions if that helps
if you have control over both js and java versions, then normalise the object before json-encoding it (either in both or at least one of them)
try to use a java JSON lib that closely emulates the js version (or vice-versa), eg GSON
write your json-encoder (either in java,js or both), its not that hard

reading a json object in jsp

i have a JSON object passed to the jsp page. it is passed as a string. now i have to parse this string and retrieve the values that are passed through the JSON object. so that i can print the values in the same jsp.
There's lots of resources, including libraries & plugins for various technologies/frameworks on json.org.
With tons of JSON parsers, it comes down to how you want to deal with data in JSON. My personal favorite from the lot is Jackson, but many others work well for simple cases too, including the "reference implementation" (aka JSON.org parser).
(I assume you want a Java parser, given reference to jsp)
My preferred solution to this problem involves using a JSON parser that provides an output that implements the java.util.Map and java.util.List interface. This allows for simple parsing of the JSON structure in the JSP Expression language.
Here is an example using JSON4J provided with Apache Wink. The sample imports JSON data from a URL, parses it in a java scriptlet and browses the resulting structure.
<c:import var="dataJson" url="http://localhost/request.json"/>
<%
String json = (String)pageContext.getAttribute("dataJson");
pageContext.setAttribute("parsedJSON", org.apache.commons.json.JSON.parse(json));
%>
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}
To make this clean, it would be preferable to create a JSTL tag to do the parsing and avoid java scriplet.
<c:import var="dataJson" url="http://localhost/request.json"/>
<json:parse json="${dataJson}" var="parsedJSON" />
Fetch the name of the node at index 1 : ${parsedJSON.node[1].name}

Categories

Resources