Problems on xml to json convertion using org.json library - java

We have used org.json library to convert xml data to json data
the code is very simple like below
JSONObject obj = XML.toJSONObject(xml);
In some cases it throws exception as
org.json.JSONException: JSON does not allow non-finite numbers.
We are processing huge amount of data at a time. there is more above ten thousand xml data are converting to json data. But a 10 or 15 are failing and throws the exception I mentioned above.
But when I took that xml file which throws the exception and run it as a single data it get succeed. We cannot figure out why is this happenning. If anyone used this library please give some advices.

Related

How to convert JSON object received in response to REST Client request to a MT940 Swift text file in Java?

How to convert sample JSON given below to a MT940 txt file:
This JSON would be a bad sample to show but hope you get the gist of it...
Just like we have a library in place to parse MT940 strings/txt we also have a library which can help construct a MT940 txt file in Java.
{
"accNumber":"123356",
"openBalInd":"D",
"openBalaDate":"200605",
"curr":"Dollar",
"transactions":[
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"
},
{
"amount":""434,
"credit/debit":"1000",
"datetime":"20042020"}]
}
MT940 is SWIFT message type.
Your input is JSON and the output is MT940 text file.
It is always good to have some java model classes representing your json.
Deserialise json input using Jackson to model you gonna use internally.
Use your own library or some third party like https://www.prowidesoftware.com/resources/SWIFT-writer to convert your internal model to MT940
Serialize the result into text file.

Does anyone know how to change response error format in Spring security oauth2?

I am using Spring security oauth2. By default oauth2 returns it's own error format like {error : "Invalid_grant", error_description : "something"}. I want to change it my own custom format so in my application, it remains consistent. Can anyone please help me? I have gone through lots of links but didn't find any suitable solution till now.
What you get as a result is a JSON document.
Look at Jackson or Gson libraries for example to parse(deserialize) JSON documents. You can get data values 1 by 1 or deserialize into a class instance.
Once you parse modify the data as you wish
Use the same library Jackson or Gson to write(serialize) a new JSON document.
Jackson may also produce as output XML, YAML and CSV documents
https://github.com/FasterXML/jackson-core
https://github.com/google/gson

How to pull individual pieces of data from a JSON output String?

In Java, how would I pull the data for just the "data" section in the following JSON output?
{"getuserhashrate":{"data":1425,"runtime":8.2659721374512,"version":"1.0.0"}}
json.get("data") does not work and gives the following error:
Exception in thread "main" org.json.JSONException: JSONObject["data"] not found.
at org.json.JSONObject.get(JSONObject.java:454)
at so4308554.JsonReader.main(JsonReader.java:40)
You have a JSON object. It contains exactly one field: getuserhashrate
{"getuserhashrate": ... }
That field contains another JSON object which has its own fields ("data", "runtime", etc).
{"data":1425,"runtime":8.2659721374512, .. }
From looking at your stack trace, you're using the basic json.org library (or the Android SDK).
You'd parse the JSON and get back the top-level object via:
JSONObject root = new JSONObject(myJsonString);
Then you'd get the object contained in the getuserhashrate field:
JSONObject data = root.getJSONObject("getuserhashrate");
Now you are able to access the fields of that object.
JsonPath is a good tool to extract information from JSON
{"getuserhashrate":{"data":1425,"runtime":8.2659721374512,"version":"1.0.0"}}
String json = yourStringjson;
String data = JsonPath.read(json, "$.getuserhashrate.data");
data will be 1425
Just add the libs and it will be a useful tool if you dont want to create beans to have a full marshall
https://code.google.com/p/json-path/

Reading lots of data returned by API

I am reading from an API provided by a company, but the problem is that one of the accounts from which I am getting the data has around 22000 json objects, it reads fine with small amounts of data, i would say up to 8000 records, but then I get issues like the json is not well formatted besides the problem of being able to read the response.
The response comes this way:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ywers.com">
[{"Name":"Edward", "LastName":"Jones", "Address":"{accepted}"}
,{"Name":"Carlos", "LastName":"Ramirez", "Address":"{Rejected}"}, ....... 22k more records here]</string>
I asked for some help earlier on here for the best way to do this, and i got a response about reading it using the xml parser and then a json parser, i am using GSON.
String XML = "<Your XML Response>";
XPathExpression xpath = XPathFactory.newInstance()
.newXPath().compile("/*[local-name()='string']");
String json = xpath.evaluate(new InputSource(new StringReader(XML)));
and then
JSONArray jsonRoot = new JSONArray(json.trim());
System.out.println(jsonRoot.getJSONObject(0).getString("Address")); // {accepted}
The problem with this is approach i am having is that it throws errors when reading the XML, it starts reading but after a while it stops with errors like:
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractBuilder.java:94)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at org.apache.harmony.xml.dom.CharacterDataImpl.appendData(CharacterDataImpl.java:43)
......
I would appreciate any advise on how to proceed with this, I am kind of new to android.
I don't know who would wrap 22k objects inside a xml string, but apparently someone is doing that. From my experience, your out of memory is because the you try to convert all the response to string but the response is too big to be handled. I recommend you to stream the JSON data. You can do stream the JSON data from the inputstream response that you get from the your HTTP post, but you need to skip the XML part by creating another input stream from the original response input stream and skip the XML part
Before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for alternative you can also use Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)

Android HTTPPost body parsing

I am having a really challenging time parsing some XML data returned to my Android app.
The data is sent as XML but printing it on my mobile screen, it comes out as the following:
{"sessid":"5eed0b52c6953b52e262b559b5557be4","session_name":"SESS6cbf091341a26e4687fa7850b465755a,"user":{"uid":"15","name":"guest","pass":"084e0343a0486ff05530df6c705c8bb4","mail":"adeoduye#hotmail.com", "mode":"0","sort":"0","threshold":"0","theme":"","signature":"","signature_format":"0","created":"1306008217","access":"1306094503","login":"1306134979","status":"1","timezone":"3600","language":"","picture":"","init":"adeoduye#hotmail.com","data":a:1:{s:13:\"form_build_id\";s:37:\"form-49ea7a4ef10a8a2b31478696f17e8dee\";","form_build_id":"form-49ea7a4ef10a8a2b31478696f17e8dee","roles":{"2":"authenticated user","3":"guest"}}}
Can anyone please help a newbie and give me some ideas on how to parse this type of output and/or plain XML?
This isn't XML but JSON. You have to parse that string using the JSON API.
Basically you create a JSONObject by feeding the string into a JSONTokenizer. You can now query the values from the JSONObject as described in the API reference example.
The String you're seeing here is in JSON format. You can parse this in Andriod using the following library : http://code.google.com/p/google-gson/
For more info on json, checkout http://json.org.

Categories

Resources