How to deserialize JSON object on the client side - java

I'm using Netty Socket IO to send socket.io content from Java (well, actually Scala.) I've tried several ways of sending a JSON object to the client:
hardcoded String
standard Java json library
Gson library
Each time, when the data gets to the client, it's just a string that looks like a JSON object. I'm currently sending it with:
case class Data(message:String) and gson.toJson(new Data("what up")). The object looks like this on the client: "{"message":"what up"}", although it's just a String and not a JSON object.
Any ideas on how to get an actual JSON object on the other side?
Thanks

This works (JQuery):
$.parseJSON(string)

Related

How to send data to kryonet outside java

trying to understand how to work with kryonet, it is quite easy to send and receive message from java client, but what if I want to send it from some UI tool like Hercules, or not java code. As I see it uses kryo for Serialization, is there way to serialize object to this fromat without java? Or use plain String or json for comunication?
You can easily create a server using json instead of kryo:
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.serialization.JsonSerialization;
new Server(16384, 2048, new JsonSerialization());

How can I send an Array as a parameter for making body for requesting to the server?

I have to send some parameters for making body for requesting to the server.
like this:
"UserName":"a.m",
"CityId":"1",
"UserCategories":
[
{"CategoryId":"ab2d948a-59d1-420d-a29b-8fd88c2d637c"},
{"CategoryId":"237b6334-3c1f-44ac-bd87-a8e6be0b2144"}
]
I don't know how can I send UserCategories as an array.because it is not String that I can send it like other parameters for making body.
I should have the body like this for getting response:
body: {"CityId":"1","UserName":"a.m","UserCategories":[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]}
but now my body is:
body: {"CityId":"1","UserName":"a.m","UserCategories":"[{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"}
I'm really confused!
When you send or receive JSON data, it is always as a string. We might speak of JSON objects or JSON arrays, but they are really string representations of objects and arrays. You can send the data exactly as you have written here as long as you enclose the entire thing in {}. You can send just the array in string form if you wish, exactly as you typed it here. The sender should create a string from the array and the receiver should parse the JSON string into an array. There are many libraries available to help you do this in every language.
your existing UserCategories object is List/Array format after that changed in String format so only its happen
"UserCategories":"[
{"UserCategories":"4211f3f4-f506-4458-b96a-0b496515e019"},
{"UserCategories":"df7487b3-2043-46ec-97d4-790bfbe83cfc"}]"
this is your second-time Json, Here see your List sent with ""
like "UserCategories":"[]"
Yes. You can do that by using retrofit library. You can sent object,arraylist using Retrofit.
https://futurestud.io/tutorials/retrofit-send-objects-in-request-body.Check this url to sent Object. You can also sent arraylist under Object In body

Jetty servlet: How can I forward GET request with parameters as POST request with JSON body?

My web clients send GET requests with URL query parameters. The receiving App can only accept POST requests with JSON body. I would like to embed a jetty servlet to the receiving App which converts GET requests to POST request, with url parameters being converted to json format body.
Input GET url for example: http://localhost:8081/?key_1=value_1&key_2=3value_2...&key_n=value_n
Expected POST json payload: {"key_1":"value_1", "key_2":"value_2", ..."key_n":"value_n"}
Could you please illustrate how to implement such functions?
I`ve worked with other programming languages, but am completely new to java. I really really appreciate your help.
Thanks and best regards,
Fischlein
You can read all the query string parameter and put it into HashMap. Then serialize this hashmap using jackson-json api or google gson api.
Jackson Reference Url :
http://wiki.fasterxml.com/JacksonHome
Read the parameters from the get request, create a json string and post it with a utility library like http://hc.apache.org/httpclient-3.x/

Json string as input parameter to ksoap

I have a soap web service and I am consuming that web servie from a android device using ksoap.
I have a java object and I used google gson library to convert that object to json like below
Gson gson=new Gson();
String agr0=gson.toJson(myobject);
Now I am adding thia string as input parameter to ksoap request as
SoapObject request= new SoapObject();
request.addPreperty("arg0",arg0);
When I call my web service using HttpTransportSE call method I get EOFException I beleive it is because of special characters in arg0 string.
At the server side I am using gson to parse json string to myobject.
How to resolve this exception.
Well SOAP uses XML as its data format. Not JSON. I would imagine that the issue is that you are getting XML back from the service and the "special characters" you're referring to are coming from that.

Passing POJOs vs Strings (json encoded) to my applications WebApi

I have some forms and databases that will fill up some POJO's with data. Using Json (gson) I"m going to upload the data to my web server. The server will send back a responses that will eventually be placed back in objects.
My question is, should I encode/decode my objects to json and then pass strings back and forth to my WebApi class? Or should I pass my WebApi class my object and have it pass back an appropriate response object (based on the method I call.)?
So at when I call my Api that has all the http conection stuff in it, it would look something like this.
myWebApi postSomeData( jsonData ); //
Should jsonData be a pojo that will be encoded into json string inside myWebApi or should I encode it into json and pass in a string.
Or in otherwords
String postSomeData( String jsonData){
web code here..
}
or
ResponsePojo postSomeData( PostData myData){
...
myMapper.MapFrom(myData); //converts pojo to json
...
webcode
}
At some point there will be images included in the data needed to be uploaded.
I would recommend using json for data transfer, it will make your services technology independent.
You can use flexjon to serialize java objects in a pretty direct way on the server side.
For images you will need to do post http POST anyway, this should be done separately from data submit since an error in a image upload should not result in data loss. Typically you'll associate the id of your data object in the uploaded image (or reversly), so you need to insert your object to db and wait for the response to insert the dependency id to your image.
Best Regards,
Zied Hamdi
http://1vu.fr

Categories