我有以下输出:
Array (
[0] => stdClass Object (
[id] => 20
[news_title] => Startup finance docs in GitHub
[news_url] => http://venturebeat.com/2013/03/06/fenwick-west-github/
[news_root_domain] => venturebeat.com
[news_category] =>
[news_submitter] => 4
[news_time] => 2013-03-06 11:20:03
[news_points] => 0
)
[1] => stdClass Object (
[id] => 21
[news_title] => The problems with righteous investing
[news_url] => http://gigaom.com/2013/03/07/the-problems-with-righteous-investing/
[news_root_domain] => gigaom.com
[news_category] =>
[news_submitter] => 4
[news_time] => 2013-03-08 09:14:17
[news_points] => 0
)
)
Run Code Online (Sandbox Code Playgroud)
我如何在这些中访问类似news_url的内容?我试过这个,但无济于事:
print_r $this->$record[0]->news_title;
Run Code Online (Sandbox Code Playgroud) 首先让我说我是一个初学者(几天前开始)与golang,我正在学习如何实际应用该语言.我的目标是构建一个Web Rest API,用于查询数据库并将数据提供给用户.
我已经能够成功地创建使用马提尼(一个简单的API https://github.com/go-martini/martini),并连接到使用MySQL数据库https://github.com/go-sql-driver/mysql.我目前的问题是如何将API请求中的变量param传递给我的查询.这是我目前的代码:
package main
import (
"github.com/go-martini/martini"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
)
func main() {
db, err := sql.Open("mysql",
"root:root@tcp(localhost:8889)/test")
m := martini.Classic()
var str string
m.Get("/hello/:user", func(params martini.Params) string {
var userId = params["user"]
err = db.QueryRow(
"select name from users where id = userId").Scan(&str)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
return "Hello " + str
})
m.Run()
defer db.Close()
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的目标是获取输入变量(user)并将其插入到名为userId的变量中.然后我想根据该变量查询数据库以获取名称.一旦完成所有工作,我想回复用户的名字.
有人可以帮忙吗?当我继续学习Go之旅时,我将非常感激!
好吧,所以我做了一些搜索,没有运气.我希望这里有人能指出我正确的方向.我有一个我正在使用的JSON提要,它应该输出各种数据.目前,它只发送回所有变量的"UNDEFINED"响应.这是我正在使用的JS:
$("#loaduserdata").click(function(){
$("#userdata tbody").html("");
$.getJSON("trendFetch", function(data){
$.each(data.list, function(i, data){
var jsondata = data.action;
console.log (jsondata);
});
}
);
Run Code Online (Sandbox Code Playgroud)
我不确定问题出在哪里,因为控制台没有给我任何错误或任何理由认为JSON格式不正确:http://i.imgur.com/ySpdR.png
无论它值多少,这里是我用来生成JSON的代码 - 也许这个问题就是这个问题?
$curl = curl_init();
$url = 'http://api.site.com';
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
if($resp){
echo $resp;
header("Content-Type: application/json", true);
}
else {
echo 'Error - no response!';
}
curl_close($curl);
Run Code Online (Sandbox Code Playgroud)
编辑 - 包括JSON输出:
{
"status": "ok",
"list": {
"list_id": "2gz",
"title": "Test List",
"description": "description text...",
"image": [
"http://t2.gstatic.com/images?q=tbn:ANd9GcTz6_4aV6oHsI2kgJRRoSFCTWbew5ChTeBrAmXYh4Gez2J7usm8nwMOsA",
"http://cdn.list.ly/logos/default-list-image.png"
], …Run Code Online (Sandbox Code Playgroud)