我有这段代码来读取json对象。我需要轻松地遍历“输出” /数据/概念键中的所有元素。
有更好的方法吗?
此外,我该如何访问值的属性。即value.app_id,value.id..etc
代码
package main
import (
"encoding/json"
"fmt"
)
var jsonBytes = []byte(`
{"outputs": [{
"data": {"concepts":
[{"app_id": "main",
"id": "ai_GTvMbVGh",
"name": "ancient",
"value": 0.99875855}]
}}
],
"status": {"code": 10000, "description": "Ok"}}`)
func main() {
var output map[string]interface{}
err := json.Unmarshal([]byte(jsonBytes), &output)
if err != nil {
print(err)
}
for _, value := range output["outputs"].([]interface{}) {
//fmt.Println(value.(map[string]interface{})["data"].(map[string]interface{})["concepts"]).([]interface{})
//fmt.Println(value.(map[string]interface{})["data"].(map[string]interface{})["concepts"])
for _, value := range value.(map[string]interface{})["data"].(map[string]interface{})["concepts"].([]interface{}){
fmt.Println(value)
}
}
//fmt.Printf("%+v\n", output)
}
答案 0 :(得分:0)
最好的方法是将JSON解组为结构并遍历值,
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/" >
<html>
<head><title>Vendor Information</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>Order Information</th>
</tr>
</thead>
<thead>
<tr>
<xsl:for-each select="orderform1/customers/customer">
<tr>
<td><h4>Customer Information</h4></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="mail_a"/></td>
<td><xsl:value-of select="c_num"/></td>
</tr>
</xsl:for-each>
</tr>
</thead>
<thead>
<tr>
<xsl:for-each select="customers/customer/product_information">
<tr>
<td><h4>Product Information</h4></td>
<td><xsl:value-of select="product_description"/></td>
<td><xsl:value-of select="catgory"/></td>
<td><xsl:value-of select="upc"/></td>
<td><xsl:value-of select="p_vend"/></td>
<td><xsl:value-of select="c_information"/></td>
<td><xsl:value-of select="cost"/></td>
<td><xsl:value-of select="retail_cost"/></td>
</tr>
</xsl:for-each>
</tr>
</thead>
<thead>
<tr>
<xsl:for-each select="orderform1/customers/customer/product_information/vendor">
<tr>
<td><h4>Vendor Information</h4></td>
<td><xsl:value-of select="bus_name"/></td>
<td><xsl:value-of select="contact_information"/></td>
<td><xsl:value-of select="b_address"/></td>
<td><xsl:value-of select="vendor_id"/></td>
</tr>
</xsl:for-each>
</tr>
</thead>
<thead>
<tr>
<xsl:for-each select="orderform1/customers/customer/product_information/vendor/orders/order">
<tr>
<td><h4>Order Information</h4></td>
<td><xsl:value-of select="date"/></td>
<td><xsl:value-of select="c_num"/></td>
<td><xsl:value-of select="pur_deatails"/></td>
<td><xsl:value-of select="total_before_tax"/></td>
<td><xsl:value-of select="tax"/></td>
<td><xsl:value-of select="total_after_tax"/></td>
<td><xsl:value-of select="item_count"/></td>
</tr>
</xsl:for-each>
</tr>
</thead>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>