Getting JSON data from a .java file - java

I have a Java Method, in a class called by a Java Servlet, that returns an Array of JSONObjects. For example, if I loop through the array in a standard for loop, each object is printed as you would expect a JSONObject to appear.
I am struggling to understand how to use the .getJSON() Jquery method to get this data. Somehow, I need to get these JSONObjects to be called by this method, so I can populate a DataTable (using AJAX).
{"lastName":"doe","requestVar":3,"name":" John","rqTime":"1402600668949"}
{"lastName":"doe","requestVar":4,"name":" Jane","rqTime":"1402677126117"}
Currently, the only answers I can find are when people use the .getJSON method on a .JSON file; is there any way for me to .getJSON from a .java file?

In the .getJSON method, you can call a URL. So make a servlet call in this URL. Make that servlet return the JSON Array as string. In Javascript, get the output and use it.

Related

How to get all fields through method service.files().get(id).execute() which returns google drive File in Google Drive V3 in java?

While getting all fields for files is possible for list of files by using the method service.files().list().setFields("files"), setFields() method not working with parameter "files" for service.files().get(id) , it gives error invalid field.
So what should be used in setFields("") method to get all fields for service.files().get(id)??
We will have to set all the fields we want in the response file object in the method setFields() and those will be comma separated. It will do our work but I didn't find any single parameter that can do this work if we want all the fields.

How to validate the csv headers using opencsv

I am using opencsv to parse csv file data which has been uploaded using web and populating the read data in the bean (using HeaderColumnNameTranslateMappingStrategy) which is working fine.
But struggling to find best way to validate (as a first check) that if the file has all the headers before starting processing the data.
Opencsv still process the file and populate null values in the bean when the file does not have all the headers that have been passed as a columnsMapping map.
So given a CSV file you want to ensure that the header contains a set of required elements before processing.
I would create a utility class with a readHeader method that takes the file name and using the CSVReader read the header using the readNext() as an string array (instead of skipping over it) and returns it.
Then you can add a second method that takes that array and an array or list of required fields and then using something like the Apache Commons ArrayUtils make sure that each element in your required array is in the header array and return true if so, false otherwise.
Then if you want you can create a third method that combines the two to hide the complexity .
Hope that helps.

Web service response is MessageElement?

I am trying to figure out how to use this service, which should print out holidays in US: http://www.holidaywebservice.com/Holidays/HolidayService.asmx?WSDL
So, I generated Java classes for it, and tried to call its method, which should return list of available countries:
holidayServiceLocator.getHolidayServiceSoap().getCountriesAvailable().get_any()
getAny() method returns org.apache.axis.message.MessageElement[] type of object, and this is where I am lost.
As I can understand, MessageElement is used in order to store XML, am I correct? In that case, how should I handle it in order to get correct result (list of supported countries for this service)?
You can either use MessageElement.getElementsByTagName(String name) if you know the tag names in the response or you can use MessageElement.getChildElements() to iterate through all of them one by one.

How do I print json structure for a multi-level json using java

I am very new to JSON and am not sure if the term "multi-level" json is correct. If not, please help correct it.
I have been tasked with printing the request and response structure of a given rest service. I have the api.json which refers to a host of json objects, which in turn refer to other json objects and so on...
Please note that I am interested in printing the structure and not the contents of the request and response.
I know that I can go ahead and do a recursive read of the files and get this done. But that does not seem right.
Can someone please provide some pointers for the same?
There is a general misconception about JSON. JSON is a format to represent a complex data structure as a string. That's it. JSON itself doesn't know anything about structure - it's really just a string. What you can do is parse the JSON string to get JavaScript objects (i.e. numbers, strings, arrays and things of type object).
This means that you can't really learn enough about the structure of the rest service by looking at JSON strings it accepts or sends. You need to look at the documentation or the internal objects which the service uses to parse the JSON instead.
Example:
{"foo":"bar"}
That tells you that the REST service accepts a JavaScript object and one of the possible parameters. But it doesn't tell you about the other 50 parameters.
If you only have the JSON, then you can use a parsing library to turn that into something that you can print. But unless you want pretty printing (indent and such), that's the same as printing the JSON string itself, so you don't gain anything.

Storing and Retrieving List of ParseUsers

In using parse, I'm trying to save an
ArrayList of < ParseUser >
in the 'value' of a 'key' for a specific ParseObject. As per my understanding, this is possible, and I am doing it simply by passing in an ArrayList as the value and calling saveInBackground().
Looks like the save is successful and is stored in the data browser like this:
[[{"__type":"Pointer","className":"_User","objectId":"fHwBwBkLtn"}]]
To retrieve this object, looks like when I do Object.get("array_key") it seems to return a JSONArray instead of an ArrayList of ParseUsers.
How can I directly parse this (excuse the pun) into an ArrayList?
You will need to tell it to include the users in your query, have a look at the documentation, there's a section that talks about the include method.
query.include("users");
Now when you call resultObject.get("users") you should get an ArrayList of ParseUser objects.

Categories

Resources