- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有下一个 JSON 数据:http://jsonblob.com/532d537ce4b0f2fd20c517a4
所以我试图迭代的(就像 PHP 中的 foreach 一样)是:invoices -> invoice(是一个数组)
所以,我想做的是:
package main
import (
"fmt"
"reflect"
"encoding/json"
)
func main() {
json_string := `{"result":"success","totalresults":"494","startnumber":0,"numreturned":2,"invoices":{"invoice":[{"id":"10660","userid":"126","firstname":"Warren","lastname":"Tapiero","companyname":"ONETIME","invoicenum":"MT-453","date":"2014-03-20","duedate":"2014-03-25","datepaid":"2013-07-20 15:51:48","subtotal":"35.00","credit":"0.00","tax":"0.00","tax2":"0.00","total":"35.00","taxrate":"0.00","taxrate2":"0.00","status":"Paid","paymentmethod":"paypalexpress","notes":"","currencycode":"USD","currencyprefix":"$","currencysuffix":" USD"},{"id":"10661","userid":"276","firstname":"koffi","lastname":"messigah","companyname":"Altech France","invoicenum":"","date":"2014-03-21","duedate":"2014-03-21","datepaid":"0000-00-00 00:00:00","subtotal":"440.00","credit":"0.00","tax":"0.00","tax2":"0.00","total":"440.00","taxrate":"0.00","taxrate2":"0.00","status":"Unpaid","paymentmethod":"paypal","notes":"","currencycode":"USD","currencyprefix":"$","currencysuffix":" USD"}]}}`
var dat map[string]interface{}
if err := json.Unmarshal([]byte(json_string), &dat); err != nil {
panic(err)
}
invoices := dat["invoices"]
fmt.Println("\nJSON-VALUE:",json_string)
fmt.Println("\nVar type:",invoices)
fmt.Println("\nVar type using REFLECT:",reflect.TypeOf(invoices))
for index,value := range invoices.invoice {
fmt.Println(index,value)
}
}
但我收到如下错误:invoices.invoice 未定义(类型接口(interface) {} 没有字段或方法 invoice)
http://play.golang.org/p/8uTtN6KtTq
所以,我需要一些帮助,这是我使用 Go 的第一天。
非常感谢。
最佳答案
当您执行 invoices := dat["invoices"]
时,invoices
的类型是 interface{}
,它代表 could是任何东西。
实际上它是一个map[string]interface{}
。要将 interface{}
转换为具体类型,您需要使用 type assertion像这样。
for index,value := range invoices.(map[string]interface{}) {
fmt.Println(index,value)
}
参见 the playground完整的例子。
但是,如果这个结构定义明确(并非所有 json 都是),那么我会定义一个 struct and unmarshal to that这将使您的代码更易于阅读。不要忘记为结构名称使用大写字母,然后使用 json:"name"
标签命名它们(参见 tags in the Marshal section of the docs )
关于戈朗 : How can i access json decoded field?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575847/
我得到了一些有趣的结果,试图辨别使用 Encode::decode("utf8", $var) 之间的区别。和 utf8::decode($var) .我已经发现,在一个变量上多次调用前者最终会导致错
我尝试使用 FlushedInputStream :Android decoder->decode returned false for Bitmap download 但没有任何变化,因为我使用:B
我有一小部分代码: from pyasn1.type import univ from pyasn1.codec.ber import decoder decoder.decode(binary_fi
这个问题在这里已经有了答案: Instantiated optional variable shows as nil in Xcode debugger (2 个答案) 关闭 2 年前。 在 Swi
我在 Playground 中有以下示例代码。如果结果符合 Decodable 协议(protocol),我想解码网络请求的结果。 知道为什么这段代码不起作用吗? protocol APIReques
我正在尝试使用 imagecreatefromwebp() 将 webp 文件转换为 JPEG,但不幸的是,它向我发出警告:警告:imagecreatefromwebp():WebP 解码:无法解码输
我试图覆盖 JSONDecoder 解码数据的方式。 我尝试了以下方法: struct Response : Decodable { init(from decoder: Decoder) t
ACTIVATE_THIS = """ eJx1UsGOnDAMvecrIlYriDRlKvU20h5aaY+teuilGo1QALO4CwlKAjP8fe1QGGalRoLEefbzs+Mk Sb7
我正在尝试使用 swift 4 来解析本地 json 文件: { "success": true, "lastId": null, "hasMore": false,
我的代码有问题。 我正在尝试使用ExtJS和Codeigniter制作上传文件格式。 这是我的下面的代码, Ext.require([ 'Ext.form.field.File',
我有一些遗留代码正在调用 sun.net.www.ParseUtil.decode()。我想避免调用供应商特定的函数,所以我想用其他东西替换调用。 我可以使用 java.net.URLDecoder.
使用 Sonatype Nexus,我仅在访问 /nexus/#admin/support/status 时收到此错误消息. Ext.JSON.decode(): You're trying to d
我正在学习 Elm,让我感到困惑的一件事是“Json.Decode.succeed”。根据docs succeed : a -> Decoder a Ignore the JSON and produ
有什么区别 URLDecoder.decode(String s) 和 URLDecoder.decode(String s, String enc) 我有一个 cookie 值,例如 val=%22
使用 Google Apps 脚本,我想解码 HTML,例如: Some text & text ¢ 存储为: Some text & text ¢ 所以,类似的问题:How t
我正在对带有字幕的视频进行编码,但出现错误“解码的字幕文本中的 UTF-8 无效;可能缺少 -sub_charenc 选项。解码流时出错”,但视频还是编码了。忽略此错误的后果是什么?谷歌搜索显示一个人
我有如下代码: cn_bytes = [157, 188, 156] cn_str = "" clen = len(cn_bytes) count = int(clen / 3) for x in r
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
This script give you a decoded listing from an encoded file. Supports *,je, ,vbe, .asp, .hta, .htm,
telnet客户端响应如何解码 我认为这是一个特定的响应,因为所有思科服务器都有相同的响应.这段文字的名称是什么,我如何解密它 '\xff\xfb\x01\xff\xfb\x03\xff\xfd\x1
我是一名优秀的程序员,十分优秀!