| View previous topic :: View next topic |
| Author |
Message |
johne Voice
Joined: 19 Jul 2005 Posts: 29
|
Posted: Wed Feb 15, 2012 2:12 pm Post subject: json2dict help |
|
|
i'm trying to pull [dict get $data watched show title] from the data below after using json2dict. what am I missing to pull the watched data via dict or is there a better way?
http://pastebin.com/AEk3feYz |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Feb 15, 2012 3:39 pm Post subject: |
|
|
Looking at the json-data shows that the watched-element contains a tcl-list, not a dict. Considder using something along these lines (assuming $source contains the json-source)
| Code: | set mydict [json::json2dict $source]
foreach show [dict get $mydict watched] {
putlog "title: [dict get $show show title]"
} |
or
| Code: | set mydict [json::json2dict $source]
putlog "title: [dict get [lindex [dict get $mydict watched] 0] show title]"
|
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
johne Voice
Joined: 19 Jul 2005 Posts: 29
|
Posted: Wed Feb 15, 2012 4:53 pm Post subject: |
|
|
| worked great! thx! |
|
| Back to top |
|
 |
elbandido Voice
Joined: 01 May 2012 Posts: 4
|
Posted: Tue May 01, 2012 2:11 am Post subject: dict with list |
|
|
I'm trying to extract id or name from this json source:
| Code: | | [{"id":2020512,"name":"Romper"},{"id":5882783,"name":"Romper amante"},{"id":5630419,"name":"Romper the last"},{"id":5630428,"name":"Romper the Last II"},{"id":5465877,"name":"Romper5"}] |
[dict get $json_data id] seems to not working, any help regarding that? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue May 01, 2012 9:46 am Post subject: |
|
|
@elbandido:
First off, did you convert your json-data to a tcl-list/dict collection using the json::json2dict command?
Secondly, your set of data will return a list of dicts, so you'll either have to iterate through each of them using foreach, or select a specific index using lindex before using dict to get the id property. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
elbandido Voice
Joined: 01 May 2012 Posts: 4
|
Posted: Tue May 01, 2012 3:45 pm Post subject: |
|
|
yes, like this:
| Code: | package require http
package require json
bind pub * .search search
proc search {nick uhost hand chan argv} {
variable url0 "xxx"
set url "${url0}${argv}/1.json?key=xxxxx"
set json_data [::json::json2dict [::http::data [::http::geturl $url]]]
}
|
I want to extract first value for "id" from json data above. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue May 01, 2012 3:50 pm Post subject: |
|
|
As I said in my previous post, the json-data you posted will translate to a list of dicts... Use lindex or foreach to extract the dict(s) from the list before using the dict-command on it/them. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
elbandido Voice
Joined: 01 May 2012 Posts: 4
|
Posted: Tue May 01, 2012 4:04 pm Post subject: |
|
|
Solved:
| Code: | | [lindex [lindex $json_data 0] 0] |
|
|
| Back to top |
|
 |
|