This comes from someone who doesn't really know Java.
I have a .json file that contains an array of objects. It is guaranteed that the file is formatted in a correct manner. Is there a simple way to deserialize the entire contents of that file into a List<myObject> (and then serialize it back as an array).
I saw all kinds of code that is more complex than it should, or treats each key in an individual manner, which is something that I don't really need.
My suggestion would be:
in order to get rid off a lot of code and boilerplate use an already developed lib like Gson, Jackson or similar,
take a look how you can model the list you are trying to read... and try to write a POJO Class (online tools can help you to do that)that represents the objects in the list.
try to serialize and deserialize the file's content and
the rest is just enjoy the results...
Related
"{\"statusCode\":400,\"throttle\":{\"retryAfter\":null,\"intRetryAfter\":0,\"throttled\":false},\"code\":\"FAILED\",\"description\":\"{\\\"statusCode\\\":400,\\\"code\\\":\\\"FAILED\\\",\\\"description\\\":\\\"\\\\\\\"{ \\\\\\\\\\\\\\\"response\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESS\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"SUCCESSFUL MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":123},{\\\\\\\\\\\\\\\"code\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"NOT_FOUND\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"description\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"FAILED MESSAGE\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"errors\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"Error\\\\\\\\\\\\\\\":{\\\\\\\\\\\\\\\"message\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"Id not found\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"reason\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"ID_NOT_FOUND\\\\\\\\\\\\\\\"}}],\\\\\\\\\\\\\\\"Id\\\\\\\\\\\\\\\":1234}]}\\\\\\\"\\\",\\\"throttle\\\":{\\\"retryAfter\\\":null,\\\"intRetryAfter\\\":0,\\\"throttled\\\":false}}\",\"adId\":0}"
I am receiving a response from a network call, which contains an array with a combination of success and failure responses, I would like to extract them out from this escaped JSON. In the above example I got two code one is a SUCCESS CODE and other is a FAILED CODE, I would like to extract them out and map to a common class.
I tried parsing this JSON using StringEscapeUtils.unescapeJava(stringToBeParsed), but I am still not able to map all the fields, is there a more efficient way to do it? where I can extract response array and map individual field to a common class ?
PS: I am new to java, and not sure about an efficient way to do this here. Writing my own parser would be an overkill to this problem I feel.
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
I was wondering if it was possible to store a hash map with a special key function (similar to the solution posted by Jon Skeet at Using a byte array as Map key) and thus data wrapper inside the android internal storage
And how to get them out again.
Namely, the data underlying it all is char [], but that char [] is wrapped around in this custom class that is used in the hashmap.
The value part is simple string, but the key is the important bit where I need the data inside it to be preserved on each opening of the app.
Do I need to overwrite certain functions in the wrapper to make sure it works with the FileOutputStream? How do I import it back again?
Use Gson to convert the map to a JSON string. Then write that string to disk. To get it back, use Gson again. Prob could accomplish this 5 lines of code. No messing with convertors or parsing needed.
I don't have a direct answer for you. However I can suggest simply saving your hashmap as an xml file. Writing the xml is pretty easy. You will need to write the higher level parts of parsing to read it back.
I'm new to Java. I want to send an array (ArrayList) of objects over the network via Java Web Service to my Silverlight app. This ArrayList contains custom class objects:
ArrayList<SVNSearchResult> results
so I'm thinking the best way is to serialize this to an XML String and on the Silverlight part, use LinQ to parse it. If there's a better way to send it please let me know. Thanks.
XML is a good fit for this. JSON would be one of the other usual suspects these days.
Whatever format you end up choosing, make sure you get the encoding right.
For a starter, try JSON. It has a network-efficient format, and is supported by any major language in the world.
XML is only my second choice as it is more complicated to generate/parse and is more verbose.
Is there any way to deserialize in PHP an object serialized in Java? IE If I have a Java class that implements Serialization and I use an ObjectOutputStream to write the object, and convert the result to a string, is there a way in PHP to take that string and create a similar object representation from it?
What does the Java Serialized data look like?
Response:
���sr�com.site.entity.SessionV3Data���������xpsr�java.util.HashMap���`��F�
loadFactorI� thresholdxp?#�����w������t� sessionIdt�0NmViMzUxYWItZDRmZC00MWY4LWFlMmUtZjg2YmZjZGUxNjg5xx
:)
I would heavily recommend you don't do this. Java serialization is meant for a Java instance to both save and load the data (for either transmission to another Java application or persistence between invocations of the same application). It was not at all meant to be a cross-platform protocol.
I would advise you to make an API adapter layer between the two. Output the contents of your Java object to a format you can work with in PHP, be it XML, YAML, or even a binary format (where you could use DataOutputStream).
What is the easiest way to eat soup with chopsticks when the soup was put in a bowl with a ladle? Put the soup in a cup and discard your chopsticks, because chopsticks are a poor choice for aiding in the consumption of soup. A cup (ubiquitous) eliminates external dependencies except for "mouth" and "opposable thumbs", both of which come with the standard library of humans.
A more elegant solution would be to encode that Java object with a JSON Serializer or XML serializer. Protocol Buffers or any other intentionally cross-language serialization technique would work fine plus Protocol Buffers can efficiently encode binary data.
Some time ago i did something simillar. However i didn't make PHP read "Java serialize" format. I did the oposite, that is, made Java serialize itself to a "PHP serialize" format. This is actually quite easy. Have look at PHPSerializedResponseWriter class that is a part of Solr package:
https://github.com/terrancesnyder/solr-analytics/blob/master/solr/core/src/java/org/apache/solr/response/PHPSerializedResponseWriter.java
...then all you have to do is just read the string and call:
$result = unserialize($string);
From comments in the online PHP manual, there is a Java class that serializes to the PHP serialization format that you can look into. Then you can unserialize the data using the standard PHP functionality.
Is it possible to use one of the more common cross platform data formats like JSON to communicate between your Java app and PHP? PHP has plenty of parsers for those formats. Check out json_decode for an example.
Is there any way to deserialize in PHP
an object serialized in Java?
Yes. The question is, should you? Exporting the Java object as XML or JSON probably makes more sense.
The following SO question might also help.
Dynamically create PHP object based on string