Parsing automatically gerenerated JSON file - java

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

Related

IBM Integration bus, parsing json

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.

Easy way to convert complex json to simple POJO files for Android

I'm trying to convert complex json response to simple POJO objects. What I mean, I can convert this json response to pojo with www.jsonschema2pojo.org , but it generates me 47 files. I don't want this solution.
I want simple and light POJO objects. So, what I need, a few pojo files (May be max 5-6 files) with entire json attributes.
Example
My sample json response ( Wordpress ):
https://pastebin.ca/3855759
And the generator creates these files:
see image
I read these and similar posts but I could't fix my issue;
stackoverflow.com/questions/41692991/convert-nested-json-to-simple-json
stackoverflow.com/questions/31117784/convert-complex-json-to-one-level-pojo-using-jackson
So, do you have any solution to accomplish this?
try GSOnFormat plugin from in android studio plugins.
you can install it from file >Settings>plugins>browse repository>GSONFORMAT
use it using alt+insert

Convert one json format to another in java

I am looking for a utility which converts one json format to another by respecting at the conversion definitions from a preferably xml file. Is there any library doing something like this in java ?
For example source json is:
{"name":"aa","surname":"bb","accounts":[{"accountid":10,"balance":100}]}
target json is :
{"owner":"aa-bb","accounts":[{"accountid":10,"balance":100}]}
sample config xml :
t.owner = s.name.concat("-").concat(surname)
t.accounts = t.accounts
Ps:Please dont post solutions for this example, it is just for giving an idea, there will be quite different scenarios in mapping.
Is this what u need?
Open input file.
Read / parse JSON from file using a JSON library.
Convert in-memory data structure to new structure.
Open output file
Unparse in-memory data structure to file using JSON library.

How to convert List<Object> to JSON on playframework Java

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

parse XML to POJO using XStream

I have an xml file which I want to read and parse it into POJO.
I am using XStream for this.
I am not able to send the file as an input to the code for parsing(file is on my local drive).
How to read the xml file and parse it using fromXML() method ?
I would be gratefull if someone can give example for sending xml file as input and parsing it to POJO and printing it on the screen
Thanks...
Based on the provided comments, I can better understand the problem you are facing. I believe the answer you are looking for is implicit collections. Check out the XStream tutorial on aliasing: http://x-stream.github.io/alias-tutorial.html .

Categories