I have json data with a colon in the label (see responsedata) which I'm finding difficult to access in Angular with the following code:
<li ng-repeat="i in items.autnresponse.responsedata | searchFor:searchString"> <p>{{i.autn:numhits}}</p> </li>
I keep getting an error like this:
Error: [$parse:syntax] Syntax Error: Token ':' is an unexpected token at column 7 of the expression [i.autn:numhits] starting at [:numhits].
JSON data excerpt:
"autnresponse": {
"action": {
"$": "QUERY"
},
"response": {
"$": "SUCCESS"
},
"responsedata": {
"autn:numhits": {
"$": "92"
},
"autn:totalhits": {
"$": "92"
},
"autn:totaldbdocs": {
"$": "188"
},
"autn:totaldbsecs": {
"$": "188"
},
Does anybody know a way around this?
I'll assume I know the answer to my question from the comments and post what would be my response:
Assumption
Your JSON parses fine but your code can't access something in the resulting data structure
Answer
Use square bracket notation with a string:
var x = i['autn:numhits'];
The same can be used when you have a property name in a variable. Using your same example:
var propertyName = 'autn:numhits';
var x = i[propertyName];
Addendum
For Angular template, try
{{i['autn:numhits']}}
Use brackets to access it like a dictionary rather than dot notation. Replace {{i.autn:numhits}} with {{i['autn:numhits']}}
As a heads up, if you want to wrap autn:numhits with double quotes you will need to html escape them.
Related
I need to parse JSON string.
I've tried JSON.stringify and then JSON.parse below sample string, but server performed escape sequencing
I used str.replace('/\\/g','') to remove the escape sequence but that doesnt help because if you look in the "default_request" key is wraps its value with "" which is doesnt allow me parse it using JSON.parse()
{
"request": {
"service_name": "authService",
"url": "https://some-url.com/{accounts}",
"default_request": "{\"authMethod\":\"somename\",\"multiCheck\":false}"
}
}
so I tried to replace "{ with { and }" with }
str.replace('/"{/g','{')).replace('/}"/g','}'))
but it creates another problem.
Favourable condition
{
"request": {
"service_name": "authService",
"url": "https://some-url.com/{accounts}",
"default_request": {\"authMethod\":\"somename\",\"multiCheck\":false}
}
}
default_request was stringifyied twice. to fix it, try this
jsonObject.request.default_request = JSON.parse(jsonObject.request.default_request);
I have JSON object containing the array of Hashmaps as:
{ "someparentkey": {
"arraykey": [
{
"uniquekey": "key1",
"content": "param1"
},
{
"uniquekey": "key2",
"content": "param2"
}
]
}
}
Based on each value of "uniquekey", I want to render the separate dust template with value of "content" passed to the imported dust template.
This is what my base template looks like right now:
{#someparentkey}
{#arraykey}
{#select key={uniquekey} }
{#eq value="key1"}{>"path/to/dust1" param={content} /}{/eq}
{#eq value="key2"}{>"path/to/dust2" param={content} /}{/eq}
{#default}<!-- Invalid script tag {key} in configuration -->{/default}
{/select}
{/arraykey}
{/someparentkey}
And my imported templates ("path/to/dust1.dust" and "path/to/dust1.dust") are like:
<span>{param}</span>
But while doing the "grunt build" of the dust file, I am getting the error as:
SyntaxError: Expected end tag for arraykey but it was not found. At line : 3, column : 9
Blockquote
Warning: Dust.js failed to compile template "path/to/my/base-dust".
Questions:
What's the issue in my current template code?
And is there any better way to achieve what I want?
This is the template which I finally ended up using and it worked for me:
{#someparentkey.arraykey}
{#select key=uniquekey }
{#eq value="key1"}{>"path/to/dust1" param=content /}{/eq}
{#eq value="key2"}{>"path/to/dust2" param=content /}{/eq}
{#default}<!-- Invalid script tag {key} in configuration -->{/default}
{/select}
{/someparentkey.arraykey}
I'm unable to successfully get data from the JSON string below. Using JavaScript, I'm able to alert the full string [ alert(data); ] but I'm unable to get only the first name.
Can someone please help?
var data = {
"name": [
"Enid Norgard",
"Cassie Durrett",
"Josephine Ervin"
],
"email": [
"TheWoozyGamer#gmail.com",
"TheHabitualGamer#gmail.com",
"TheUptightGamer#gmail.com"
],
"role": [
"Gamer",
"Team Leader",
"Player"
],
"emp_id": [
"50",
"408",
"520"
],
"id": [
"234",
"444",
"235"
]
}
Looks like you have a string(because when you use alert the complete text is shown, if it was a object then [Object object] would have shown), first you need to parse it using JSON.parse()
var t = JSON.parse(data)
alert(t.name[0])
Note: Old browsers like IE8 which does not have native support for JSON you have to add a library like json2 to add JSON support
use the following code
alert(data.name[0]);
//sample code
var json = '{"result":true,"count":1}',
obj = JSON.parse(json);
alert(obj.count);
For the browsers that don't you can implement it using json2.js.
Most browsers support JSON.parse(), hope this will help you for detail see link.
With data.name[0] you will get the name Enid Norgard
Similar to that use
data.name[index]
while index is the position of the name in the innerarray.
If you want only the names array use:
alert(data.name)
try this to loop through all elements
for(x in data)
{
for(y in data[x])
{
alert(data[x][y]);
}
}
The JSON file I have is following:
{
"root": {
"Quizsize": null,
"{urn:abc.com/xmlns/mona/page}page": [
{
"#xid": "page1623",
"#title": "Quiz Landing Page",
"{urn:abc.com/xmlns/mona/page}comment": null,
"{urn:abc.com/xmlns/mona/page}skills": null,
"{urn:abc.com/xmlns/mona/page}info": {
"{urn:abc.com/xmlns/mona/common}property": {
"#name": "quiz_landing",
"#value": "true"
}
}
}
]
}
}
}
I am loading this JSON file using :
var jsondata = eval("("+chapterRequest.responseText+")") ;
Root = jsondata.root
Now I want to access #xid which is "page1623 and #name which is "quiz_landing". I dont know how to do this,Please help.
Thanks
JSON.parse(x) is better than eval(x), for one. It is not supported natively by some browsers though. If you want to access #xid, "urn:abc.com/xmlns/mona/page}page" points to an array whose first element is an object.
So use Root["{urn:abc.com/xmlns/mona/page}page"][0]["#xid"]. Or mix bracket and dot notation. Your choice, really.
When the key isn't a valid identifier, you use object['key'] instead of object.key, so jsondata.root['{urn:abc.com/xmlns/mona/page}page'][0]['#xid'].
I am sending a JSON object from a servlet to JSP using AJAX. My JSON object contains a String value inside. and that string contains double quotes within that. My JSON does not parse it. I get the following error:
{"diagnosis":[{"NAME":"new_diagnosis_1 \[1020\]:2000000006001"},{"NAME":"new_diagnosis_2 \[1021\]:2000000006003"},{"NAME":"new_"dise"sed \[1023\]:2000000009001"},{"NAME":"new_d"ise"sef \[1024\]:2000000009003"}]}
note new_"dise"sed and new_d"ise"sef
I need a solution.
your json is not valid
try this
{
"diagnosis": [
{
"NAME": "new_diagnosis_1 [1020]:2000000006001"
},
{
"NAME": "new_diagnosis_2 [1021]:2000000006003"
},
{
"NAME": "new_\"dise\"sed [1023]:2000000009001"
},
{
"NAME": "new_d\"ise\"sef [1024]:2000000009003"
}
]
}
use \ to escape quotes
you can validate your json here
http://www.jsonlint.com/