Parsing JSON Array with DynamicForm Play framework 2.2.6 - arrays

I have JSON array of Objects that comes from user page
{[1].id=10, [0].name=banana, [1].measurementSystem=g, [1].name=bacon, [0].id=2, [0].cal=23, [1].cal=23, [0].measurementSystem=g}
How can I get all IDs from this object array into a integer Java array?
I tried this:
DynamicForm data = Form.form().bindFromRequest();
for (String s : data.data().values()){
System.out.println(s);
}
But it returns the array of all values.
So how can I get those ids only?

If the DynamicForm isn't working, I'd suggest trying to just parse the JSON as JSON. You can get a JsonNode object from the request using this code:
JsonNode json = request().body().asJson();
Using this you can then process the JSON to pull out whatever data you need.
Working with JSON in Play should be documented here.

Related

Why is the array behaving like this

I am working on Angular 6 and i want to post an array of JSON objects using a service class.
The following is my function
getrerun(data,file){
this.post=[];
for(var i=0;i<data.length;i++)
{
this.rerun.storeno=data[i].storeNo;
this.rerun.filetype=data[i].loadtype;
this.rerun.outboundsystem[0]=file;
this.rerun.createdate=data[i].createdDate;
this.post[i]=this.rerun;
console.log("------>"+JSON.stringify(this.rerun)+"------->"+JSON.stringify(this.post));
}
this.newurl=this.devurl+"rerun";
return this.http.post<any>(this.newurl,this.post);
}
newurl is the url of the rest api that i want to hit and this.post is the array that i am sending
The value of data is following:
[{"lastDate":"2019-02-20 12:36:27","storeNo":"G015","country":"SG","serviceStatus":"FAIL","createdDate":"2019-01-04 11:53:56","loadtype":"F"},{"lastDate":"2019-02-20 10:54:00","storeNo":"G121","country":"SG","serviceStatus":"FAIL","createdDate":"2019-01-23 16:29:33","loadtype":"F"}]
and file is 'TP';
However the post array that I am getting is this:
[{"outboundsystem":["TP"],"storeno":"G121","filetype":"F","createdate":"2019-01-23 16:29:33"},{"outboundsystem":["TP"],"storeno":"G121","filetype":"F","createdate":"2019-01-23 16:29:33"}]
this basically means that both the entries in the array are the same i.e this.post[0].storeno is same as this.post[1].storeno. However, they should have two different values.
What do I do about this?

Compare data between two Arrays containing custom Swift Objects

I have some data I have received through an API that return JSON to me. I know I can fetch it and store relevant info from the API into my iOS app. But only while the app is running. I.E. I have not implemented YET how to store the info fetched from the API into UserDefalults. Working on this feature I ran into a problem.
I have two Arrays that keeps track of my data. The first Array is the Array I want to store in UserDefaults when I have fetch my data. This one is called "lenders" and keeps LenderData
The second Array is my temporary Array. It contains the same type of objects, and this is the one I want to populate with data from the API and then compare to my existing Array "lenders".
I want to check if the "lenders" Array contains any object that has the same id as the object I'm looking at in the "lendersTemp" array. If the lenders Array does not contain any LederData object with the id of the tempLender we are currently looking at, we add the tempLender into the lenders Array. How would I go about doing this?
My current (non-working) solution is as follows:
var lenders = [LenderData]()
var lendersTemp = [LenderData]()
...
// Get JSON DATA
...
for tempLender in self.lendersTemp {
if !self.lenders.contains(where: {$0.id == tempLender.id}) {
self.lenders.append(tempLender)
}
}
EDIT:
My view did load method:
var lenders = [LenderData]()
var lendersTemp = [LenderData]()
override func viewDidLoad() {
super.viewDidLoad()
downloadJSON {
self.myTableView.reloadData()
}
self.myTableView.rowHeight = 90
myTableView.delegate = self
myTableView.dataSource = self
}
I figured it out with some help! So this is my answer to my own question!
My problem was that getting data from my API is done with an asyc method. And I tried to do comparison after the reload method was called on my TableView. So I did not populate the array the tableview is getting data from, before after the reloadData() had been called and therefore it seemed like my tableview and comparison algorithm, did not work, when in fact it did!

How to save array of objects in MongoDB.in a Node.js application

I am trying to upload an Excel file to MongoDB database.
I used below NPM package manager to convert Excel data to MongoDB
mongoXlsx.xlsx2MongoData(path, model, function(err, data){
console.log(data); // (This variable data has an array of objects, each object is a row in Excel.)
});
The image attached has the data.
Now, I want to upload this 'data' (That has an array of objects) into MongoDB. How can I do that? Please suggest.
You can use a for loop to iterate through the array , and insert each element of the array in a single mongoDB document :
for ( var counter=0 ;counter<data.length;counter++)
{
dataBaseName.colletionName.insertOne(data[counter]);
}
but before that, as #zenwraight pointed out, you should convert your data into correct json format , for example Name : 'ABC' should become 'Name' : 'ABC' (same goes for other fields)

Get each JSON array value using PHP

I've been trying to read this JSON with PHP:
{
"total_found":"49",
"1":{
"title":"Maze Runner-The Scorch Trials 2015 HD-TS x264-Garmin",
"category":"video",
"seeds":2141,
"leechs":1176,
"torrent_size":1122230176,
"torrent_hash":"29d3e7062825a62abdd877ee96dc3fea41183836"
},
"2":{
"title":"Maze.Runner-The.Scorch.Trials.2015.720P.HD-TS.x264.AC3.HQ.Hive-CM8",
"category":"hdrip",
"seeds":395,
"leechs":1856,
"torrent_size":3999751475,
"torrent_hash":"e1e14a5ccf540a739907db2e1e0d810ecdb8bebc"
}
}
How can I get each title and torrent_size?
The easiest thing to do is to decode the JSON to an array. PHP has a built-in function for this:
$results = json_decode($json_data);
http://php.net/manual/en/function.json-decode.php
To clearly see the array structure, try dumping the variable $results using print_r();, i.e.:
print_r($results);
Then you simply need to access the data using the array.
It will be something like $results[1]['torrent_size'];.

Can i return an array of "number, number" in json format?

i have a list of lat/long objects on my server.
eg.
public class LatitudeLongitude
{
public float Latitude;
public float Longitude;
}
simple.
now, can i return a collection of these, in json format .. BUT ... i do not want to list the key, just the values.
This means the normal result would be something like ...
{ { lat: 111, long : 222 }, { lat: 333, long : 444 } } ..
but i'm hoping for...
{ {111, 222}, {333, 444} ..... }
{ {obj1.Lat, obj1.Long}, {obj2.Lat, obj2.Long} ... etc. ... }
Is this possible? I mean, i can make that string on the server side easily. But is that a correct JSON output format?
Can someone please confirm, etc.
cheers :)
PS. I hardly know any Json, so please don't hesitate to correct my poor examples above.
Use JSON arrays:
[ [111, 222], [333, 444], ... ]
Ayman's proposal isn't quite correct JSON syntax. As an object {..} is always composed of fields having a name. So we again would have object with 2 fields of type 'array' that don't have a name -> not valid json for an object.
There is a very good description of how a json string can look like at www.json.org. Looking at this description you'll see an object is not allowed without a name String for a field so this {111,222} is not a valid object in json, but this is valid [111,222] array. This is not a valid object {[111,222]} but this is {array1 : [111,222]}.
I guess what you actually want is most likely an array of arrays like this not encapsulated as an object.
[[111,222],[333,444]].
This approach would give you the choice to add as much arrays of lat/long as you like to the enclosing array and at the same time you can process the inner arrays no matter what size the outer array is with an javascript loop.
Hope that helped.
cheers
Michael
If you need to access it using the keys in JSON, you'll need to specify it (or use a JS to convert arrays to objects on the client side). Otherwise, arrays should work:
var json = [ [123, 456], [234, 567] ];
var convertedJson = [];
for (i = 0; i < json.length; ++i) {
var thisObj = new Object();
thisObj.latitude = json[i][0];
thisObj.longitude = json[i][1];
convertedJson[i] = thisObj;
}
But is that a correct JSON output
format?
You can paste in JSON at jslint.com to validate your JSON
The ASP.NET MVC JSON serializer should convert a IList<LatitudeLongitude> to something like this:
[{"Latitude":111,"Longitude":222},{"Latitude":333,"Longitude":444} .. ]
I believe that under the ASP.NET MVC JSON serializer is just a hot-wired .NET core Javascript serializer.
I dont think it's possible from .NET using a serializer. Prob need to render the string manually.

Resources