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.
Related
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 totally new on working with JSON.
Currently, I am processing JSON files, which are automatically generated from a device. The file contains huge number of objects and looks like the following:
{"age":13,"name":"Alan","Acc":[12,25,60]}
{"age":33,"name":"Dave","Acc":NULL}
{"age":18,"name":"Sara","Acc":[2,3,4]}
...
I went through some tutorials and understood JSON notations/syntax shall not be like the files that I'm working with.
I am trying to parse these files using Java JSON.simple, but I get error after the following line of code:
Object obj = parser.parse(new FileReader("/home/alan/test.json"));
The error is:
Unexpected token LEFT BRACE({)
I would be appreciated if someone could help me with this.
Cheers,
Alan
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.
First of all, I am using playframework 2 with Java.
I have a Bootstrap-lookahead input field in a view and I want to use a json array as source for it, as described here.
I can generate json at server-side with:
Json.toJson(users)
or on client with:
#{Json.toJson(users)}
However it generates strings with " and when I try to create the bootstrap-lookahead field with this data, it gives me
Uncaught SyntaxError: Unexpected token &
Could someone help me with that?
Thank you
You can prevent escaping by using #Html(Json.toJson(users)) in template.
Docs, last paragraph
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.