I am working on an angular project I want pass an array object as an parameter to another page through rouuting my function is as follows
Goto(item)
{
console.log(item.numtopics);
this.router.navigate(['/numbersystem'], { queryParams: item.numtopics});
}
and getting the data as follows
constructor(private route: ActivatedRoute) {
this.route.queryParams.subscribe( param => {
console.log(param);
});
}
but in console I am getting data like this
{0: "[object Object]", 1: "[object Object]", 2: "[object Object]"}
I am not getting the array data properly
any help.
Thank You !!
If you want to get this passed array, you should use:
console.log(param.numtopics)
or you can also pass array like this:
this.router.navigate(['/numbersystem'], { queryParams: item.numtopics});
and then you can get this array like you tried:
console.log(param)
Refer the official documentation, how queryParams are passed.
The reason why you get data like {0: "[object Object]", 1: "[object Object]", 2: "[object Object]"} is because it's an object.
Try passing JSON.stringify(item.numtopics) to the queryparams. This will pass a string to the queryParams. Also you can mention the NAME of the query(Refer Doc). For exapmle :
https://www.yourdomain.com/search?NAME=stringifiedString
Before you pass the stringified array you may want to encrypt the data so you can use some encryption algorithm. Thereafter, in the component where you retrieve the params you can again use JSON.parse(paramsData) to get array back from the string.
In case if you've used encryption, you should decrypt first and then use JSON.parse()
Related
I receive a JSON object that looks like this:
this data is being passed through a Socketio emit, so the angular HttpClient is not intercepting it.
{
"single":[
{
"name":"Xavi555",
"data":[
{
"_id":"5e2ea609f8c83e5435ebb6e5",
"id":"test1",
"author":"someone",
"recipient":"someone",
"content":"test",
"msgId":"Testid",
"gNamespace":""
}
]
}
],
"group":[]
}
however when I use JSON.parse() on the object above The key data does not contain the value of data that is to say:
private func(jsonObj: string): void {
console.log(jsonObj);
}
I see:
single: Array(1)
groups: Array(0)
single Array(1)
name: test
data: Array(0)
I thought it was an issue with deep cloning, but, when trying to do JSON.parse(JSON.stringify(jsonObj)) it returns the original json string object.
related but no solution posted
actual code:
private handleStoredMessages(dirtyObj: string): void {
// debugger;
const cleanObj = JSON.parse(dirtyObj);
const { single, group } = cleanObj;
console.log('raw json', dirtyObj);
onsole.log('clean json', cleanObj);
}
any ideas?
If you are using angular, then I doubt you should be using JSON.parse. This is handled by the HttpClient. Nevertheless, your assumption about your data structure is wrong. The single is an array of objects, not an object itself:
So to access the .data you need to do jsonObj.single[0].data. Which in itself is an array again
The only other reason this could happen, is because you modify the object/array somewhere else in your code, before you actually press the triangle in console to open the object. Hover on the blue information icon to see why.
Value below was evaluated just now
The object is lazily evaluated, meaning that if you did any transformations to the object, it will show that state, not the state at the moment you logged the object.
The issue of this was that the back end guy double encoded and did some weird stuff not my fault
I have a JSON object returned in my console, and I want to display those data named "offers".
the JSON object is returned like that :
I tried to display my JSON Object data with :
console.log(JSON.stringify(data));
The thing is, it says that "data is not defined"
Does anyone know what happens ? :)
You should add full path to element of json, for example if your json looks like:
var json = {"par":22, "par2":555, "elems":[{"attr1":53, "attr2":99}] };
and if you want to get attr1 value, you should do something like this:
console.log(json.elems[0].attr1); // 53
so in your case that could be something like:
variableName.result.data.offers //variableName is variable that your "consoling"
Method JSON.stringify doesn't get yout specified value from JSON structure, it's converts JSON object to string.
console.dir provides a good representation of object than console.log().U can try with both
console.log(result.data.offers[0]);
console.dir(result.data.offers[0]);
I have a document on a mongodb on Heroku. Each object in the document has a system generated object id in the form of
"_id": {
"$oid": "xxxxxxxxxxxxxxxxxxxxxxxx"
}
When I make a query and get the response from the server, I stringify the response using JSON.stringify and I log the object on the server console. When I do this the following gets logged:
this is the response: [{"creator":"al","what[place]":"home","what[time [start]":"22:00","what[time][end]":"","what[details]":"","who[]":["joe","kay","mau"],"_id":"xxxxxxxxxxxxxxxxxxxxxxxx"}]
Right after the full object gets logged, I try to log the id to make sure I can access it... I want to then pass the id to a different object so that I can have a reference to the logged object.
I have this right now:
var stringyfied = JSON.stringify(res);
console.log("this is the response: " + stringyfied);
console.log("id: " + stringyfied._id);
but when the item is logged I get
id: undefined
instead of
id: "xxxxxxxxxxxxxxxxxxxxxxxx"
No matter how I try to access the _id property, I keep getting undefined even though it get printed with the console.log for the full object
I've tried:
stringyfied.id
stringyfied["_id"]
stringyfied["id"]
stringyfied._id.$oid
stringyfied.$oid
you need to use JSON.parse(), cause JSON.stringify() is to convert to string, parse is to get the object. stringifiedid is a string
What is being returned is an array with one object in it.
The way to access the _id is with stringyfied[0]._id. However, it would be cleaner to pull the object out of the array instead.
There are a few ways you can do that. If this query will only ever return one result and that's all you'll want, you can use the findOne method instead. If the query may return more than a single document, then you will want to loop through the returned results.
I also agree with #dariogriffo that you'll need to use JSON.parse() on the stringified JSON variable.
my JSON response is as given below
JSON response is
{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}.
i want to take the data correspond to 'message'.i used JSON Array.and got response as
JSON array response is
[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]].
Now how can i access each array in that?
You should expand on what you have done, what language you are using, etc. Normally, you should be able to index into the array with the standard notation. In python for example you can do something along the lines of json_data["message"][0] to access the first array and json_data["message"][1] to access the second.
something like :
var d = JSON.parse('{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}')
and then you can access each array in message part as :
d.message.forEach(function(obj) { console.log(obj); });
So my issue is this.
Using backbone to save something in a MYSQL Database.
When I call this.model.save() I am getting a very weird issue.
The model will save the JSON response as an object and will not update the new values instead.
So my attributes in development tools will look something like this.
attributes: Object
0: Object
ID: "4"
Name: "TEST"
Title: "MEOW"
Stuff: "1"
When: "2013-02-14 22:17:14"
The 0 should not be there. I did confirm that the json object is valid so I know that is not the issue here.
It looks like your JSON response is actually an array with a single element, not an object.
The property 0 is created when Backbone calls model.set(response), which in turn copies all keys of the response object to the attributes hash. If an array is passed to set, this is what happens.
You should fix your server to respond with a raw object ({...}) instead of an array ([{...}]). If you're not able to change the server behaviour, you can override Model.parse to unwrap the response on the client:
var Model = Backbone.Model.extend({
parse: function(response) {
return _.isArray(response) ? response[0] : response;
}
});