func (self *HttpDownloader) downloadJson(p *robot.Page, req *robot.Request) *robot.Page { var err error p, destbody := self.downloadFile(p, req) if !p.IsSucc() { return p } var body []byte body = []byte(destbody) mtype := req.GetResponceType() if mtype == "jsonp" { tmpstr := goutils.JsonpToJson(destbody) body = []byte(tmpstr) } var r *simplejson.Json if r, err = simplejson.NewJson(body); err != nil { mlog.LogInst().LogError(string(body) + "\t" + err.Error()) p.SetStatus(true, err.Error()) return p } // json result p.SetBodyStr(string(body)).SetJson(r).SetStatus(false, "") return p }
func (self *HttpDownloader) Download(req *robot.Request) *robot.Page { var mtype string var p = robot.NewPage(req) mtype = req.GetResponceType() switch mtype { case "html": return self.downloadHtml(p, req) case "json": fallthrough case "jsonp": return self.downloadJson(p, req) case "text": return self.downloadText(p, req) default: mlog.LogInst().LogError("error request type:" + mtype) } return p }