I am trying to port this bit of Python code to Java http://www.bemasher.net/archives/1002 (scroll down to the bottom for the code)
But because everything is dynamically typed, I'm having difficulty porting this. I need to be able to use a Java implementation of Pythons json. Right now I'm using gson ( http://code.google.com/p/google-gson/ ), but I'm open to anything.
Neither of gson's fromJson's signatures
public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException
match Pythons
data = json.loads(response)
This is the JSON that I am trying to read: http://www.ows.newegg.com/Stores.egg/Categories/1
How would I go about getting the class or type of the JSON above and read through it? All the other examples I've seen you have to know what it is.
Thanks!
It basically fetches http://www.ows.newegg.com/Stores.egg/Menus and dumps its contents to a string (response), then it parses the JSON using json.loads() and puts that into data.
Then it will iterate through the contents of data and build a HTML list using lxml. The list itself should look like:
<ul>
<li>{StoreTitle}</li>
...
</ul>
Where {StoreID} and {StoreTitle} are the IDs and titles fetched and parsed from that URL.
References: urllib2, json, lxml
I forgot to mention that I was using Google Web Toolkit - sorry about that. I went ahead and accepted #NullUserException 's answer though I'm writing this for anyone else who may come across this.
When converting this from Python to the Java (GWT) equivalant, I found that there was already support in GWT for JSON, and even better it works on the client side saving trips to the server. (source: JSON GWT API)
To keep thoe code clean I am using a wrapper for it that can be found on Google Code here.
Related
I use Java (with Quarkus) for the backend.
Vue3 for the frontend.
API and Websockets to transition data between the two.
I am building a chat message application.
So far I was able to send a message inside the backend under a JSON format, do stuff in Java with this message (which holds the message inside a MessageJson class that I have created, hence the data type is now type of MessageJson), transform it into a String, then and send it back to the frontend. I wish to transform the String in the frontend, into a Json.
Here is my function which send the message back to the front (using Websockets):
public MessageResponseForm processBackend(MessageJson messageJson) {
MessageResponseForm messageResponseForm = new MessageResponseForm();
messages(messageJson);
webSocketConfig.sendMessage("U001", messageJson.toString());
return messageResponseForm;
}
The frontend retrieve it without issue, in the String format.
But I need it back into Json format.
I use the function JSON.parse() in Vue.js but it gives me this:
Which is normal in my opinion, because I should receive something like this from the backend:
"{"toRecipient":"U002","fromSender":"U00555","messageContent":"ewww555"}" .....
But instead I got this:
"MessageJson(toRecipient=U002, fromSender=U00555, messageContent=ewww555, dateCreated=2022-9-23 14:15:57, companyName=test)"
My Class MessageJson does not seems to keep the correct Json format.
How to prevent this?
Do I need to use the #Produces annotations in Quarkus? If yes, where exactly?
I found no other method related to ToString()...
EDIT: Do I need to use the dependency GSON to do the job, or can I do this natively?
I Want to Return a JSON response from server to client in gRPC.
one possible way is to convert it to string return the response then convert back to Json Object in client side, but i want to know can we do better?.
i am doing some google and found we can do it with the help of google.protobuf.struct
but didn't actually find any good example.
i want an example how i can use it as JSON in java.
If you are using proto3, one option is to define a protobuf message that mirrors the JSON object you wish to populate. Then you can use JsonFormat to convert between protobuf and JSON.
Using a com.google.protobuf.Struct instead of a self-defined message can also work. There is an example shown in the similar question.
I am trying to build my new App and I need some data for it.
so I was wondering if I can get JSON data (or query link or any source file) of any site for now and for future.
I need to get data from this site:
https://study.ekb.eg/ - https://www.ekb.eg/
Note: https://www.ekb.eg/ is the main source data for https://study.ekb.eg/
Thanks
The site needs to be specifically programmed to return JSON data for you. Regular, normal sites are not like this. They generally just return HTML
So the site owner would need to set up an API for you to consume
Hello I have a problem to parse any JSON in IIB Toolkit. The exception thrown by java compute node is: java.lang.NoClassDefFoundError: org.json.JSONObject
I am parsing incoming JSON messages in UTF-8. I already tried to get them in JSON, but accepting them as BLOB and converting to JSON UTF-8 works for me.
String messageText = new String(outMessage.getRootElement().getLastChild().getLastChild().getValueAsString());
messageText = new String(DatatypeConverter.parseHexBinary(messageText),"UTF-8");
JSONObject json = new JSONObject("{}");
I would love to create JSON object from JSON string in UTF-8
Many thanks in advance!
So what you are trying to do is a bit of a no-no. You're trying to use the Java class JSONObject rather than using the builtin IIB Java Parser.
Have a look at MbElement in particular the methods createElementAsLastChild(java.lang.String parserName) and createElementAsLastChildFromBitstream.
As per my earlier answer never forget your are trying to build a tree of elements.
One other trick I sometimes use is to build a sample output message and send it to an Input node connected to a Trace node. I then use the Trace node output to write code to build my actual output tree, you can even put a Trace node after your JavaCompute node to see what the Element tree you've currently built looks like and correct your mistakes. I mostly use this method for SOAP messages which can be quite complex.
If you really want to use external Java classes then search for Using JAXB with a JavaCompute node and follow the links from that article.
I have a couchbase database that is shared between multiple applications, storing documents as json. I cannot seem to get data into my java app, since it appears to be dependent on native java binary serialization.
This code:
CouchbaseClient client = new CouchbaseClient(hosts,"bucket","");
System.out.println((String)client.get("someKey"));
results in
net.spy.memcached.transcoders.SerializingTranscoder: Failed to decompress data
java.util.zip.ZipException: Not in GZIP format
since it is trying to deserialize by default. I notice that I can provide my own transcoder, but I really only want the raw string data so I can json parse it myself using gson or whatever. None of the available transcoders seem to give me this.
The couchbase docs have an example for setting json, but none for reading it. How are people reading json into java?
First off, this problem will go away soon in that the Couchbase "2.0 SDKs" implement common flags between each other so this kind of problem doesn't come up. Michael's blogs are a good read if you want to see what's happening here. The reason for the problem in the first place is that in the 1.x series, Couchbase was trying to stay compatible with existing application code and memcached. In the memcached world, the clients were all written by different people at different times.
Based on the exception, I believe you're probably trying to read an item stored by .NET. I have a sample transcoder you can use for this from a few weeks ago.
Make sure you are using latest CB java client:
<dependencies>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>couchbase-client</artifactId>
<version>1.4.4</version>
</dependency>
</dependencies>
see: Couchbase Java Client Library 1.4
I have my service that uses CB client running just fine. Here is how I create client:
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
cfb.setOpTimeout(10000);
cfb.setOpQueueMaxBlockTime(5000);
CouchbaseClient client = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucketName, ""));
And here is an example how I get a raw string and convert it to POJOs:
MyPOJO get(CouchbaseClient client, String key)
{
com.google.gson.Gson gson = new com.google.gson.Gson();
String jsonValue = (String) client.get(key);
return gson.fromJson(jsonValue, MyPOJO.class);
}
Also, update your question with the sample JSON doc that causing this issue. Perhaps it has something to do with the format of the document itself.