我是新来的。我一直试图使用他们的API从Flickr获取照片,但是我面临一个解析JSON响应的问题。
我一直在用Go编写服务器来处理web服务调用。我的输出如下所示:
{
"photos": {
"page": 1,
"pages": 3583,
"perpage": 100,
"total": "358260",
"photo": [
{
"id": "18929318980",
"owner": "125299498@N04",
"secret": "505225f721",
"server": "469",
"farm": 1,
"title": "❤️ Puppy Dog Eyes❤️ #Cute #baby #havanese #puppy #love #petsofinstagram #akc #aplacetolovedogs #all_little_puppies #americankennelclub #beautiful #bestanimal #puppies #cutestdogever #dog #doglife #doglover #dogoftheday",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "18930020399",
"owner": "125421155@N06",
"secret": "449f493ebc",
"server": "496",
"farm": 1,
"title": "Titt tei hvem er du for en liten tass Osvald og King #cat#kitten #bordercollie #puppy#dog",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
},
{
"id": "18929979989",
"owner": "131975470@N02",
"secret": "7da344edcb",
"server": "498",
"farm": 1,
"title": "Shame, Shame",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}
]
},
"stat": "ok"
}
当我试图运行代码时,代码显示:
cannot use jsonData.Photos.Photo[i].Id (type int) as type []byte in argument to w.Write
我的代码如下:
package main
import(
"os"
"fmt"
"log"
"net/http"
"io/ioutil"
"encoding/json"
"github.com/gorilla/mux"
)
type Result struct {
Photos struct {
Page int `json: "page"`
Pages int `json: "pages"`
PerPage int `json: "perpage"`
Total int `json: "total"`
Photo []struct {
Id int `json: "id"`
Owner string `json: "owner"`
Secret string `json: "secret"`
Server int `json: "server"`
Farm int `json: "farm"`
Title string `json: "title"`
IsPublic int `json: "ispublic"`
IsFriend int `json: "isfriend"`
IsFamily int `json: "isfamily`
} `json: "photo"`
} `json: "photos"`
Stat string `json: "stat"`
}
func main() {
router := mux.NewRouter().StrictSlash(true);
router.HandleFunc("/Index", Index)
log.Fatal(http.ListenAndServe(":8084", router))
}
func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json");
url := "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=6b54d86b4e09671ef6a2a8c02b7a3537&text=cute+puppies&format=json&nojsoncallback=1"
res, err := http.Get(url)
if err != nil{
fmt.Printf("%s", err)
os.Exit(1)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil{
fmt.Printf("%s", err)
os.Exit(1)
}
jsonData := &Result{}
err = json.Unmarshal([]byte(body), &jsonData)
for i := 0;i < len(jsonData); i++ {
w.Write(jsonData.Photos.Photo[i].Id)
}
}
发布于 2015-06-24 15:48:42
问题不在于你的模型,它运作得很好。当错误发生时,您已经完成了反序列化(没有错误),这是在这一行;w.Write(jsonData.Photos.Photo[i].Id)
。当一个int需要一个字节数组时,您就传递它了。
这个答案解释了如何进行转换;将整数转换为字节数组
所以,让你的代码成为一些工作形式;
import "encoding/binary"
buffer := make([]byte, 4)
for i := 0;i < len(jsonData.Photos.Photo); i++ {
binary.LittleEndian.PutUint32(buffer, jsonData.Photos.Photo[i].Id)
w.Write(buffer)
}
注意,这是将您的值的二进制表示形式写成一个没有符号的32位int,带有小的endian位顺序。这对你可能不管用。我不能说什么会,所以你必须在那里做一些决定,比如你想要什么位顺序,如果你需要签的还是没有签的等等。
编辑:要使上面的工作,我想你必须做一个从int到uint32的转换。更仔细地看一下我与你联系的答案,你可以这样做,这样做更干净/更简单,海事组织。
import "strconv"
for _, p := range jsonData.Photos.Photo {
w.Write([]byte(strconv.Itoa(p.Id)))
}
第二个编辑:有许多方法可以将int转换为二进制。另一个很好的选择是:func Write(w io.Writer, order ByteOrder, data interface{}) error
我相信你的代码,你可以这样用;
import "encoding/binary"
for i := range jsonData.Photo.Photos {
binary.Write(w, binary.LittleEndian, jsonData.Photos.Photo[i].Id)
}
http://golang.org/pkg/encoding/binary/#Write
编辑:更新了这三个例子,使其适用于不同品种的循环。
https://stackoverflow.com/questions/31030555
复制相似问题