Iterating PHP namespaced objects in ng-repeat - javascript

I'm trying to iterate trough array of objects using ng-repeat in Angular. Everything works fine except for an API, which returns json encoded PHP object, which resides in different namespace on server. That leads to situation, where returned object properties contains \ in name and I can't find the way how to access them while using {{Namespace\Property}} syntax in Angular. \ causes syntax error on client side, even when trying to escape this character doubling it.
Logging JSON-decoded object in browser returns correct contruction, but can't figure the way to read specified property.
Example output returned from API:
0: Object
Auth\Username: admin
Auth\Domain: acme.org
Auth\Enabled: true
1: Object
Auth\Username: guest
Auth\Domain: acme.org
Auth\Enabled: false

Related

JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native)

I have created a way to store data the way I want in AsyncStorage. This includes:
Reading a variety of input (various arrays)
Storing the input in a local this.state.object that houses multiple arrays
Stringifying this.state.object
Storing the stringified object in a temp array
Assigning the array with all objects to another state, this.state.allObjects
Stringifying 'this.state.allObjects'
Storing stringified array in AsyncStorage
The end goal of all of this is to have an array of objects that each represent a very different instance of the same type (with different parameters etc.). It may not be the most elegant approach, but it seems to store and load fine.
The issue arises when I try to parse anything from this.state.allObjects[x]. Or rather, it only occurs when I need to access it in a case that is NOT right after I load it.
During my loadFromAsync function, I am perfectly able to execute this.state.allObjects = JSON.parse(fromAsync); to get the array of stringified objects and then var display = JSON.parse(this.state.allObjects[0]).name to retrieve the name of the first parsed object of the array.
Any time outside of this function when I try to call var display = JSON.parse(this.state.allObjects[0]).name or even simpler <Text>{JSON.parse(this.state.allObjects[0]).name}</Text> I receive the following errors:
when running
const parse = JSON.parse(this.state.allObjects[0]);
//JSON Parse error: Unexpected identifier "undefined"
console.log(parse.name);
and when running
const parse = JSON.parse(this.state.allObjects[0]).name;
//JSON Parse error: Unexpected identifier "undefined" + null is not an object (evaluating 'JSON.parse(this.state.allObjects[0]).name
console.log(parse);
and lastly while running
const parse = JSON.parse(this.state.allObjects);
//JSON Parse error: Unexpected EOF + JSON Parse error: Unexpected token ','
I assume that this.state.allObjects changes somewhere within my code, or appends an extra } somewhere, though it really shouldn't. When I display this.state.object and this.state.allObjects[0], their format is exactly the same visually. Adding more objects to this.state.allObjects and displaying each stringified component also works; it is just a matter of actually parsing these components that is not working outside the original load function.
I am extremely stuck. Any advice is appreciated. If I need to change my datatype, that's fine. It's just a bit annoying to have come this far with a stringified array of stringified objects holding arrays and not be able to parse it.
Because this.state.allObjects Already JSON object.
Try JSON.stringify()

how do you access values in an array with topojson

I'm having trouble building a choropleth map and the one thing I'm trying to figure out is how to access an array from a json object:
This is what I see in my console for
var datum=topojson.feature(us, us.objects.states)
and I want to specifically capture the name under properties (eg. State name)
console.log(datum.features[2].properties.name)
this should work assuming you want the properties of second object in features array. Also, JSON object is nothing but javascript object you can access the properties like you do in vanilla js

Angular JS not parsing nested JSON key with the at (#) in the key name

I am hitting an API which retrieves a nested JSON and setting it to my $scope.data variable.
I do an ng-repeat like ng-repeat="event in data".
and try to access a value in the JSON {{event.src.#userID.title}}
There is an error
Lexer Error: Unexpected next character at columns 14-14 [#] in expression [event.src.#userID.title].
When I forcefully remove the # from the JSON returned from the API and access as {{event.src.userID.title}} it works properly.
Please help so that I can access value with the # in the key name.
The API that I hit returns a list [{"":""},{},{},{}]
{"":""} is a nested list
You have to use a different syntax to access an object property whose name isn't a valid variable name:
{{event.src["#userID"].title}}

Include pointers in array of pointers in Parse Javascript query

I know how to load the objects pointed in a pointer array in Parse (thanks to this link: https://www.parse.com/questions/include-pointer-in-array-of-pointers). However, I need to load objects pointed inside the array of pointers, too (which would be something equivalent to triple JOINs in SQL). I am querying a class that has an array of pointers, this works:
query.include("myArrayColumn.pointer");
It loads all the Post objects that are pointed inside the array. However, these Post object also have a _User pointer, and I need to get that _User object, too. I've tried:
query.include("myArrayColumn.pointer.user");
Unfortunatelly, user objects aren't fetched. Their id's are there, but trying to access anything other than id (e.g. username) returns undefined.
Is there a way to eager-load the users, or do I have to use a second query (or some other mechanism)?

Parsing incoming JSON in JavaScript - what happens to the "extended parameters"?

I am attempting to parse the incoming JSON from XBMC (Eden Beta v3), an example string from XBMC is as follows:
{"jsonrpc":"2.0","method":"Player.OnPlay","params":{"data":{"item":{"type":"movie"},"player":{"playerid":1,"speed":1},"title":""},"sender":"xbmc"}}
I am using json2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js to give me the JSON.parse and JSON.stringify functions in my application.
I am using the parse function in my code like this
var temp = JSON.parse(data);
Which is working fine for the first two properties, "jsonrpc" and "method"... so I can access those like this
temp.method
returns "Player.OnPlay"
temp.jsonrpc
returns "2.0"
However, if you look at the string sent by XBMC, it also contains what they refer to as "extended parameters" or "params", I cannot seem to track down what the JSON parser is doing with the rest of the message, or how I can access them in similar ways as the first two properties. If I try
params = temp.params
or any other way of accessing the params property, I get an Undefined error as there is no such property in the JSON object... I hope this is clear enough for someone to take a stab at helping me out. Will provide any extra info needed...
Thanks
The value of params in the JSON data is an object, so you have to access the sub-properties of that.
temp.jsonrpc
temp.method
temp.params["data"]["item"]["type"]
temp.params["data"]["player"]["playerid"]
temp.params["data"]["player"]["speed"]
temp.params["data"]["item"]["title"]
temp.params["sender"]

Categories