I'm trying to get a working JSON output (an array with x amount of objects) from a freemaker ftl file. If there is only 1 object in th array of "loggedInUsers" then the code below works. If there are more than 1, then the JSON breaks. I know a comma should separate the separate between each, but the problem comes when I add one after the closing brace. Any help would be greatly appreciated.
[
<#list loggedInUsers as user>
{
"user": "${user}"
}
</#list>
]
If I understand well, you want to add a comma except after the last item. In that case use the #sep directive, like }<#sep>,</#sep>. (See also: http://freemarker.org/docs/ref_directive_list.html)
Related
date =soup.find_all("td", {"id": "utime"})
print date
[<td class="mstat-date" colspan="3" id="utime">23.11.2015 17:00</td>]
this is what i want
[23.11.2015 17:00]
print(soup.date.string)
AttributeError: 'NoneType' object has no attribute 'string'
Little help please, thank you.
The method find_all will always return a list. In a list, you must index, before you can access methods on the elements contained within. So when you have
date =soup.find_all("td", {"id": "utime"})
You can access the text from the first tag, by typing:
date[0].text
If there are more items in the list, you can use a list comprehension, like this:
[ _.text for _ in date ]
That will give you a list of dates, if the HTML you're scraping had more than one such date-like tags.
Your beautifulsoup instance will not have associated with it a date attribute/property, unless in some very specific conditions. Use your first function to access all the dates, so don't try this approach (bad: soup.date).
I am completely stumped on this one. Everything's working fine (or fine enough for now) and all I need is to get the data back out of the factory in a non-json format. I've got a semi-working plunker with all the details.
Basically, the first page (under the Ctrl controller) is where a user can check a bunch of boxes. There's also an ng-switch between sets of options (the real things are much, much larger lists than these), so the checkboxFactory maintains the user's choices. When the user goes to the next page (or "next page" in the plunker because faking it), they can see what they chose. Those choices will then get wrapped up in a json post back to the server. I need to show the user-friendly name, but also have the id# of the choice, for the server.
If I put value="{{item.name}}" in the original checkbox ng-repeat, everything is fine. Except for the fact that then I have a factory of names, and not the server-required ids. Doing a second array in the factory (one for selected names, one for the corresponding selected ids) seems like overkill, when theoretically I could just add each selection as an object, and extract the properties as needed on the second page.
In reality, it's not working. Here's what I get if I echo the factory, after selections are made:
[ "{\"id\":1,\"name\":\"Firstplace\"}", "{\"id\":2,\"name\":\"Second place\"}" ]
...and I'm not sure, but those backslashes seem to be turning every selection into strings, because there are quotes just inside the square brackets. I've tried editing line 54 in the script, but I get errors. It doesn't like this:
if (checked && index == -1) {
if (setup) elem.prop('checked', false);
else scope.list.push({
id:"scope.value.id",
name:"scope.value.name"
});
On the html side, it doesn't like any of the variations I've tried in the ng-repeat, either. It seems like the source of all nightmares is that pushing is creating deformed json. I've tried all of these the second page/output:
{{item}}
{{item.name}}
{{item.item.name}}
The only one that works is {{item}} and unsurprisingly it's pretty ugly. Has anyone run into this before, and have any hints on how to fix this? Many thanks in advance.
using # will turn your object into a string, you should just use a reference to your item object instead and use =.
Change {{item}} to just item as a reference:
<input type="checkbox" name="group1" value="item" ng-model="isChecked" checkbox-list='checkedCity' />
In directive use =:
scope: {
list: '=checkboxList',
value: '='
},
see updated plunker
This may be a little confusing to describe.
Basically, I am parsing multiple external JSON feeds that display in different views depending on the 'active tab' displayed. They both share the same partial template, so they both look exactly the same, just different content.
The problem that I am facing now is, that in some feeds, some keys are placed in an array and others are not.
For example, the feeds parses this kind of data:
JSON Feed 1 - One 'attributes' inside of 'link'
"link":{
"attributes":{
"href":"www.link1.com"
}
}
JSON Feed 2 - Two 'attributes' inside of 'link'
"link":[
{
"attributes":{
"href":"www.link1.com"
}
},
{
"attributes":{
"href":"www.link2.com"
}
}
]
The only way I am able to get the value "www.link1.com" is via:
For Feed 1:
link1
And for Feed 2:
link1
I am trying to figure out what would be the best way to do:
1) If link[0] exists - display it, else if [link] exists, display that instead.
2) Or if targeting the activeTab would be safer? For instance, if activeTab = view2 or view4, use [link][0], else if activeTab = view1 or view3 use [link], else if I do not want it to be displayed, do not display anything.
Also a relatable question, if I am on view2 can I only display [link][0] on that view?
Any feedback would be appreciated. Thanks!
In your model controller, you can reconstruct the JSON objects to make them similar. The value of link in both feeds should be an array.
Then in your template you can simply use ngRepeat to get the items from inside the array.
Okay - so I found a solution to one of the questions above: "How to only display [link][0] in a specific view"
Pro: It's a simple code that depends on the activeTab / view that is being displayed.
Con(?): Since I am really a newbie to AngularJS - not sure if this is the best solution.
Basically:
Depending on the ng-view that is currently displayed, than a specific JSON object will be displayed, such as:
<a ng-show="activeTab == 'view1' || activeTab == 'view3'" ng-href="{{item['link'][0]['attributes']['href']}}">
<h6>Link1 from Feed2</h6>
</a>
Although the primary question is still unresolved: How to swap/switch JSON objects (key,values) if one exists, and not the other. I am still definitely trying to find a solution, although any help is still appreciated.
Please let me know what you think, or how I can improve the solution to the problem!
Thanks!
Roc.
My Question is simple: Is there anyway to display array value through its index in DUSTjs syntax.
Sample JSON :
{ names:
[
"Jhon",
"Peter",
"Curly"
]
}
with the above json sample, I just want to display any of the names through its index in DUST syntax.
Note: We are able to display all names, but i need any of the names to be displayed as output through its index (Eg : names[0] something like this or by any other way).
when iterating $idx will give you the index so for example showing them as <li> elements:
{#names}
<li>
{names[$idx]}
<li>{~n}
{/names}
otherwise just plain
{names[1]}
will give you first element
This is quite easy to achieve:
{names[0]} gives you Jhon
{names[2]} gives you Curly
and so on.. Hope this helps.
if you use the linkedin dustjs fork, you can do that:
take a look here: http://linkedin.github.com/dustjs/test/test.html. There are a lot of examples.
This is the wikki: https://github.com/linkedin/dustjs/wiki
and this is the code repo:
https://github.com/linkedin/dustjs
I'm using the Last.fm API to return data in JSON format and this works fine. I'm using the user.getTopArtist() API call.
As the page loads, a DIV object is created for each artist containing relevant details from the JSON data. When a user performs an action with the DIV I basically want to swap the image url to show a bigger image size!
How can I find/reference a JSON object by matching it's stored value?
For example, if I need to match the artist name 'Kate Bush' and then retrieve the "extralarge" image url. How would I do this?
The data structure looks like this:
{"topartists":{
"artist":[{
"name":"Kate Bush",
"playcount":"20",
"mbid":"4b585938-f271-45e2-b19a-91c634b5e396",
"url":"http:\/\/www.last.fm\/music\/Kate+Bush",
"image":[
{"#text":"http:\/\/userserve-ak.last.fm\/serve\/34\/224740.jpg","size":"small"},
{"#text":"http:\/\/userserve-ak.last.fm\/serve\/64\/224740.jpg","size":"medium"},
{"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/224740.jpg","size":"large"},
{"#text":"http:\/\/userserve-ak.last.fm\/serve\/252\/224740.jpg","size":"extralarge"},
{"#text":"http:\/\/userserve-ak.last.fm\/serve\/500\/224740\/Kate+Bush.jpg","size":"mega"}
]
}
}
What are you using to parse JSON?
Here's a jQuery example http://api.jquery.com/jQuery.parseJSON/
If it is just for this particular task then that $.each(data.topartists.artist[0].images) approach would work.
As of more generic solution... Take a look on http://goessner.net/articles/JsonPath/ - that is XPath variant for JSON