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.
Related
As a intermediate Java student I have a question regarding retrieving single data pieces with a key from a larger document, uploaded online.
My idea is to create enum objects (public static final) and associate one ArrayList to each of enums in its constructor. All of the enums (with arrayLists) would then be uploaded onto the single larger online server database.
My wish is that every enum would serve as a key to retrieve appropriate (associated) arrayList in such document; meaning I would only like to retrieve arrayList one in a time (based on the enum as a key) and not the (whole) document itself. I would then like to add or remove elements from given arrayList and upload them back online.
How can I do that in the most effective fassion possible?
Thank you for your help and have a nice day :D
As far as my understanding and Assuming you want to use Enum and Arralyslist and store the Arralylist Object in Database.Try following below
You can simply create list from array like this:
List<String> list = Arrays.asList(SomeEnum.values());
Note :- Using JDBC API you can Connect to the DB and Insert the ArrayList Object using Insert query in the Database.
I have a JSON which is mentioned below.
{"Orders"
[{"BusinessUnit":"TS",
"DeliveryDetails":
[{"SlotStartDateTime":"2015-03-30T16","DeliveryOption":"Home
Delivery","ReservationID":"13349259","PersonInfoShipTo":
{"Address":"OrganizationName":"HP","BuildingTypeID":"",
"IsCommercialAddress":"false","PostalCode":"56001",
"City":"Bangalore","AddressLine3":"A3","AddressLine2":"A2",
"AddressLine1":"A1","IsPAFValidated":"true",
"GridRef":"0473601734","State":"KA","AddressLine4":"A4",
"Country":"IN"}},"ShipNode":"NODEUK","SlotEndDateTime":"2015-
03-30T17"}],
"FulfilmentID":"a9466f83-938d-4115-a3d4-62ff4bdcd1b6",
"OrderTypeIndicator":"SalesOrder"}]}
Question:
I am able to take the values of BusinessUnit, FulfilmentID & OrderTypeIndicator. But, I am unable to take the values present inside the inner json object DeliveryDetails. Could anyone help me on taking those values
"DeliveryDetails" is mapped to a json array object with a single json object inside.
Try doing:
json.getJSONArray("Orders").get(0).get("DeliveryDetails").get(0)
Of course it would be better to check first if keys exists and the size of json arrays returned before retrieving actual indices.
I'm trying to get a value from the databae.
My Database query:
String GroupID1="select idCompanies from companies where Company_Name='ACME';";
here I'm calling to a javabeans which give back an ArrayLIst with one element
ArrayList<?> IdGroup1=oper.getList(GroupID1);
then, I print the result:
System.out.println(IdGroup1);
The query works fine, however I'm getting as a result:
[javabeans.ListOneElement#609f6e68]
Instead of the real value. How can I convert the java object to the real value?
you are printing the ArrayList object IdGroup1,You need to iterate to get the alues
This code will retrieve the first (and only) item from the list:
System.out.println(IdGroup1.get(0).toString());
Adding the following will prevent a nullPointerException:
if (!IdGroup1.isEmpty())
System.out.println(IdGroup1.get(0).toString());
-Added .toString() to get the value of the object
Consider what type of Object oper.getList(GroupID1) will return.
You need to accommodate for whatever object that is and then convert it to String.
You need to:
Unpackage your list (that is a list contains, and is expected by java to possibly contain multiple objects, so it doesn't automatically 'unpack' it for you if you have a list of 1 object)
Extract your string. Here java might cleverly convert a number (int, float, etc. ) to a string for you.
For part two, look at what you expect the object to be by finding the JavaDocs for whatever package is handling your database queries. Then see how to extract your string.
It might be as simple as System.out.println(IdGroup1.get(0).toString());
Where get(0) unpackages the object from the list, and toString() extracts the string.
If you still get back javabeans.ListOneElement#41ccdc4d try other methods to extract your string....toValue() perhaps? It depends on the packages you're using.
I have seen this question and understand the answer, but can not use it in my scenario.
My scenario: I retrieve data via JPA from a mysql database and want to put this data into a JSONObject like this
{
"A":["1","2","3"],
"B":["1","2","3","4"],
"C":["1","2"]
}
The problem is I do not know how many arrays I will retrieve. It could be 1 or it could be 200, depending on the data in the database.
If I append the data into a JSONObject like this:
import org.apache.tapestry5.json.JSONObject
// ...
JSONObject data = new JSONObject();
for (Value val : values) data.append(val.getName(), val.getValue());
I'll get
{"val_name": [[["1"],"2"],"3"], ...}
Is there a way to use JSONOBject.append without creating JSONArrays and puting them into the JSONObject, which will result in a nested JSONObject?
A JSON object is a "dictionary" -- a map between name and value. A JSON array is a sequential list of values, with only the ordinal position of the value identifying it. It makes no sense to "append" to the object -- you add new name/value pairs to it (although they apparently call it appending, just to confuse you). If, within an object, you want something like "A":["1","2","3"] then you necessarily must insert an array (as the value of a name/value pair) into the object.
But note that either before inserting the array into the object or after you can append additional values to the array. You just need to get/save a reference to the array.
In your above example you're making the mistake of appending to the object rather than the array.
I have some domain classes and i want to init and fill those classes with sample hardcode data , is there any method which i can fill data with any framework ?
For Example : List<Customer> should be filled with some mock data
Consider maintaining your test data in a JSON structure, and use a framework (e.g. google-gson) to deserialize the data into value objects.
If you wish to auto-generate random data, you might want to look into something like Quickcheck, which seems to be Java's equivalent of the .NET framework Autofixture.
As #ipavlic wrote, you might make your constructor generate some random data when the object is created.
You may store the data in a DB or a simple text file and read it from there when you fill your list.
You may combine aproach 1 and 2 and store possible field values in a file or somewhere else and fill the Object fields with these randomly chosen predefined values.
If you want fill list of Customer, there is this method Collections.fill(java.util.List, T) to fill list. This method replace current objects in list. If list is empty it won't fill.
You can put your hard-coded data in a constructor.
If it's mocking frameworks that you are after (as you indicate in comments), then take a look at e.g. Mockito.