Basic json> struct question (using "Go")

I am working with twitter api trying to get json data from

http://search.twitter.com/trends/current.json

which is as follows:

{"as_of":1268069036,"trends":{"2010-03-08 17:23:56":[{"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},{"name":"#MusicMonday","query":"#MusicMonday"},{"name":"#MM","query":"#MM"},{"name":"Oscars","query":"Oscars OR #oscars"},{"name":"#nooffense","query":"#nooffense"},{"name":"Hurt Locker","query":"\"Hurt Locker\""},{"name":"Justin Bieber","query":"\"Justin Bieber\""},{"name":"Cmon","query":"Cmon"},{"name":"My World 2","query":"\"My World 2\""},{"name":"Sandra Bullock","query":"\"Sandra Bullock\""}]}}

My structures look like this:

type trend struct {  
 name  string  
 query string  
}  

type trends struct {  
 id string  
 arr_of_trends []trend  
}  

type Trending struct {  
 as_of  string  
 trends_obj trends  
}

and then I parse JSON into a type variable Trending. I am very new to JSON, so my main task is to make sure that I have a data structure correctly configured to store the returned json data.

I write this in "Go" for a project for school. (This is not included in any specific task, I am just demonstrating something for a presentation in the language)

UPDATE: as per PeterSO comment I go on a regular route. Using:

Cur_Trends := new(Current)
/* unmarshal the JSON into our structures */

//find proper json time-name
aoUnixTime, _, _ := os.Time()

// insert code to find and convert as_of Unix time to aoUnixTime
    aoName := time.SecondsToUTC(aoUnixTime).Format(`"2006-01-02"`)
    fmt.Printf("%s\n", aoName)
    regexp_pattern := "/" + aoName + "/"
    regex, _ := regexp.Compile(regexp_pattern);

    cleaned_json := regex.ReplaceAllString(string(body2), "ntrends")
    os.Stdout.WriteString(cleaned_json)

No changes are displayed. Am I setting a regular expression incorrectly? It seems that "Go" allows only one regular expression at a time ...

UPDATE: / "ntrends", "Unmarshaling" . json.Decode, ...

, : :

map[as_of:1.268176902e+09 trends:map[ntrends:[map[name:#nowplaying query:#nowplaying] map[name:#imtiredofseeing query:#imtiredofseeing] map[name:#iWillNever query:#iWillNever] map[name:#inmyfamily query:#inmyfamily] map[name:#raiseyourhandif query:#raiseyourhandif] map[name:#ripbig query:#ripbig] map[name:QVC query:QVC] map[name:#nooffense query:#nooffense] map[name:#RIPLaylaGrace query:#RIPLaylaGrace] map[name:Justin Bieber query:"Justin Bieber"]]]]

"... " ...

+5
3

Twitter Fail Whale, Twitter API ; .

Twitter API ( ) , JSON

{
    "as_of":1268069036,
    "trends":[
        {"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},
        {"name":"#MusicMonday","query":"#MusicMonday"},{"name":"#MM","query":"#MM"}
    ]
}

as_of Unix, 1/1/1970.

Go :

type Trend struct {
    Name  string
    Query string
}

type Current struct {
    As_of  int64
    Trends []Trend
}

Twitter , JSON, :

{
    "as_of":1268069036,
    "trends":{
        "2010-03-08 17:23:56":[
            {"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},
            {"name":"#MusicMonday","query":"#MusicMonday"}
        ]
    }
}

Twitter JSON.

{
    "trends":{
        "2010-03-08 17:23:56":[
            {"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},
            {"name":"#MusicMonday","query":"#MusicMonday"}
        ]
    },
    "as_of":1268069036
}

"2010-03-08 17:23:56": - JSON. - - as_of.

"2010-03-08 17:23:56": "ntrends": ( ), as_of, JTON Twitter:

{
    "as_of":1268069036,
    "trends":{
        "ntrends":[
            {"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},
            {"name":"#MusicMonday","query":"#MusicMonday"}
        ]
    }
}

Twitter JSON "as_of":, as_of Unix JSON, :

var aoUnixTime int64
// insert code to find and convert as_of Unix time to aoUnixTime
aoName := time.SecondsToUTC(aoUnix).Format(`"2006-01-02 15:04:05":`)

Twitter JSON aoName "ntrends":, Twitter JSON.

Go, JTON Twitter :

type Trend struct {
    Name  string
    Query string
}

type NTrends struct {
    NTrends []Trend
}

type Current struct {
    As_of  int64
    Trends NTrends
}

: , .

, , , . , .

+2

, , JSON, Go . API.

"" - . , Go JSON , .

.

, Go , : http://code.google.com/p/go/issues/list

+1

.

Twitter JSON:

{"trends":[{"name":"#amazonfail","url":"http:\/\/search.twitter.com\/search?q=%23amazonfail"},... truncated ...],"as_of":"Mon, 13 Apr 2009 20:48:29 +0000"}

API- Twitter: , , , JSON, :

{"trends":{"2009-03-19 21:00":[{"query":"AIG","name":"AIG"},... truncated ...],... truncated ...},"as_of":1239656409}

, .

JSON Twitter Go, json, - :.

package main

import (
    "fmt"
    "json"
)

type Trend struct {
    Name  string
    Query string
}

type Current struct {
    As_of  int64
    Trends map[string][]Trend
}

var currentTrends = `{"as_of":1268069036,"trends":{"2010-03-08 17:23:56":[{"name":"Happy Women Day","query":"\"Happy Women Day\" OR \"Women Day\""},{"name":"#MusicMonday","query":"#MusicMonday"},{"name":"#MM","query":"#MM"},{"name":"Oscars","query":"Oscars OR #oscars"},{"name":"#nooffense","query":"#nooffense"},{"name":"Hurt Locker","query":"\"Hurt Locker\""},{"name":"Justin Bieber","query":"\"Justin Bieber\""},{"name":"Cmon","query":"Cmon"},{"name":"My World 2","query":"\"My World 2\""},{"name":"Sandra Bullock","query":"\"Sandra Bullock\""}]}}`

func main() {
    var ctJson = currentTrends
    var ctVal = Current{}
    ok, errtok := json.Unmarshal(ctJson, &ctVal)
    if !ok {
        fmt.Println("Unmarshal errtok: ", errtok)
    }
    fmt.Println(ctVal)
}
+1
source

All Articles