JSON encode array of objects - arrays

I have an array of objects like so:
[{}, {}, {}]
I need to pass this array via Socket.io. Socket.io converts the array into JSON and I keep getting the circular structure to JSON error.
Heres my current code:
for (var i = 0; i < 5; i++) {
num = randRange(0, cards[type].length);
playerCards.push(cards[type][num]);
}
socket.emit('updateCards', playerCards);
Does anyone know a way around this?
Thanks

You would see the same error if you tried to do the following:
for (var i = 0; i < 5; i++) {
num = randRange(0, cards[type].length);
playerCards.push(cards[type][num]);
JSON.stringify(cards[type][num])
}
//socket.emit('updateCards', playerCards);
The tag attribute is likely the culprit. In order for the JSON serializer to work you cannot have any circular references in the object being serialized. One option would be to pull out the information needed from the tag object and create a custom object instead.

Related

Cannot pass a string array from external data file postman automation scripts

I'm trying to pass a string array in postman- post request
postman request- body
{
"fruits":[{{fruits}}],
}
postman- test
var fruits=["mango","apple","orange"]
for (let i = 0; i < 3; i++) {
fruits.push(jsonData.fruits.toString());
}
pm.globals.set("fruits", fruits);
Then set fruits as global variables
Then run the API through the collection runner with the external data file. Then check the response body and I got this
got a 404 error
If anyone can let me know how to pass a array as a string in postman. When we pass a string array it will defined as a ascii value. So this issue has happened. So please guide me through this.
First, if you want to save an array as a variable, remember to stringify this.
let fruits=["mango","apple","orange"]
pm.globals.set("fruits", JSON.stringify(fruits));
Second, you don't need to add [ ] in request body, just put the variable
{
"fruits": {{fruits}}
}
Result:
{
"fruits": ["mango","apple","orange"]
}

How to separate the json data to push to spreadsheet?

Here is the code so far. Keep in mind the data i am passing to the function has already been parsed and stringified.
Here is my code so far. I just need to know how to push the data to the rows array and not how to move to a spreadsheet yet.
I keep getting an empty array: [17-03-29 05:07:48:351 PDT] [[null]]
function pullJSON(data) {
var rows = [];
for (i = 0; i < data.length; i++) {
var dataS = data[i];
rows.push([dataS.id]);
Logger.log(rows);
}
Here is the data that being passed to the function

Adding EventListener to an array, getting TypeError: Error #1006: value is not a function

I'm using for to addEventListeners to all of the buttons. When I do so I get the message "value is not a function". What needs to be done?
var buttons:Array;
buttons = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","Æ","Ø","Å");
for (var i:int = 0; i < buttons.length; i++)
{
buttons[i].addEventListener(MouseEvent.CLICK, onKeyPress);
}
You need to use getChildByName("name") to get the reference to the object, if the values insde the array is name of element.
Try this,
var buttons:Array;
buttons = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","Æ","Ø","Å");
for (var i:int = 0; i < buttons.length; i++)
{
this.getChildByName(buttons[i]).addEventListener(MouseEvent.CLICK, onKeyPress);
}
In this case I have used this by considering that the elements are attached on current container.
buttons[i] is not an interactive object, it is a String. You cannot add an event listener to primitive types.
You have to use either references or instance names. If "A", "B", ... are your instance names, then you have to provide the full/relative path, e.g.: root.myClip[buttons[i]].addEventListener...

Loop through an array of objects and add only the value to another array. Knockout

friends. I have the following issue. I have two observable arrays.
self.NamesArray= ko.observableArray([{"name: John"}, {"name: Mario"}]);
and
self.ValueArray = ko.observable([]);
I would like to loop through the NamesArray and add only the name values to the ValueArray.
So the output ValueArray should contain the following elements in the end:
{John, Mario}
How can this happen? I am very new to JS and I am just researching the Knockout library. Any help with working example will be greatly appreciated. Thanks.
Fiddle: http://jsfiddle.net/PsyComa/RfWVP/
This really depends on your intention behind doing this.
If you want do to this just once, simply iterate over the first array:
// Load current values of the observables
var n = self.NamesArray(), v = self.ValueArray();
// Push all names from n to v
for (var i = 0; i < n.length; i++) {
v.push( n[i].name );
}
// Update the "values" observable
self.ValueArray(v);
The downside with this is that "ValueArray" does not get updated whenever "NamesArray" changes. If you want "ValueArray" to be an array containing all names that can be found in "NamesArray" (and only those!), you can use a computed observable instead:
self.ValueArray = ko.computed(function() {
var n = self.NamesArray(), v = [];
// ...same for loop as above...
return v;
});

JSON: How do I select an array inside another array?

I have the current JSON file:
[{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
[{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}
How do I select the array with ID 1 and list every 'img_id' inside it, without repeating it for the other array?
edit 1:
I am trying to parse it like this but this code is erroneous:
$("#button").click(function() {
$.getJSON("../path/to/json", function(data) {
$.each(data[0].images.img_id, function(i,data){
var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>";
$(new_data).appendTo("#htmlTag");
});
}); return false;
});
Much appreciated.
You have an array of objects, where each object contains another array of objects. I'm assuming the JSON structure you are using is:
var a = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];
I'm also assuming you are using JavaScript.
PostEdit:
Your code is fairly close, I believe what I have below should work:
$("#button").click(function() {
$.getJSON("../path/to/json", function(data) {
$.each(data[0].images, function(i,data){
var new_data ="<p src='path/to/folder/" + data.img_id + "'></p>";
$(new_data).appendTo("#htmlTag");
});
});
return false;
});
All I did was change the first parameter to your each call from: data[0].images.img_id to: data[0].images. Also, I changed the declaration of new_data from:
var new_data ="<p src='path/to/folder/"+images.img_id+"'></p>";
to:
var new_data ="<p src='path/to/folder/"+data.img_id+"'></p>";
Note that the parameter "data" in the each callback function is simply the element in the array, while "i" is the index of that element in the array. Therefore, data is an object which looks like this:
{"img_id":1}
So, you can get the ID via data.img_id. Hope this helps.
I think you're talking about references. I don't think they are possible in JSON. In case you strictly need them and still want the readable serialization of your objects - I'd suggest you to look into YAML
I think there is a typo in your JSON...it seems like a bracket is missing at the end, and one has been added at the start of line 2. But, assuming you meant this (and are using Javascript):
var myJson = [{"id":"1","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]},
{"id":"2","images":[{"img_id":"1"},{"img_id":"2"},{"img_id":"3"}]}];
then you can access whatever you need just like a nested Javascript object. If you wanted to only access the object with ID equal to 1, and order is not guaranteed, you would have to iterate:
for(var i = 0; i < myJson.length; i++){
if(myJson[i].id === "1"){
var imgs = myJson[i].images;
for(var j = 0; j < imgs.length; j++){
//do what you want with imgs[j].img_id
}
}
}

Resources