mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve gconv to support struct field of interface{} conversion
This commit is contained in:
155
.example/encoding/gjson/gjson.go
Normal file
155
.example/encoding/gjson/gjson.go
Normal file
@ -0,0 +1,155 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func getByPattern() {
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : [
|
||||
{"name" : "小明", "score" : 60},
|
||||
{"name" : "John", "score" : 99.5}
|
||||
]
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("John Score:", j.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
}
|
||||
|
||||
// 当键名存在"."号时,检索优先级:键名->层级,因此不会引起歧义
|
||||
func testMultiDots() {
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
},
|
||||
"users.count" : 101
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("Users Count:", j.GetInt("users.count"))
|
||||
}
|
||||
}
|
||||
|
||||
// 设置数据
|
||||
func testSet() {
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
j.Set("users.count", 1)
|
||||
j.Set("users.list", []string{"John", "小明"})
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
// 将Json数据转换为其他数据格式
|
||||
func testConvert() {
|
||||
data :=
|
||||
`{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : ["John", "小明"]
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
c, _ := j.ToJson()
|
||||
fmt.Println("JSON:")
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("XML:")
|
||||
c, _ = j.ToXmlIndent()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("YAML:")
|
||||
c, _ = j.ToYaml()
|
||||
fmt.Println(string(c))
|
||||
fmt.Println("======================")
|
||||
|
||||
fmt.Println("TOML:")
|
||||
c, _ = j.ToToml()
|
||||
fmt.Println(string(c))
|
||||
}
|
||||
}
|
||||
|
||||
func testSplitChar() {
|
||||
var v interface{}
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.b.c.d.e.f.g.h.i.j.k", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
v = j.Get("a.b.c.d.e.f.g.h.i.j.k")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetSplitChar('#')
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
v = j.Get("a#b#c#d#e#f#g#h#i#j#k")
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(v)
|
||||
fmt.Println(t8 - t7)
|
||||
}
|
||||
|
||||
func testViolenceCheck() {
|
||||
j := gjson.New(nil)
|
||||
t1 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t2 := gtime.Nanosecond()
|
||||
fmt.Println(t2 - t1)
|
||||
|
||||
t3 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t4 := gtime.Nanosecond()
|
||||
fmt.Println(t4 - t3)
|
||||
|
||||
t5 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t6 := gtime.Nanosecond()
|
||||
fmt.Println(t6 - t5)
|
||||
|
||||
j.SetViolenceCheck(false)
|
||||
|
||||
t7 := gtime.Nanosecond()
|
||||
j.Set("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a", 1)
|
||||
t8 := gtime.Nanosecond()
|
||||
fmt.Println(t8 - t7)
|
||||
|
||||
t9 := gtime.Nanosecond()
|
||||
j.Get("a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a")
|
||||
t10 := gtime.Nanosecond()
|
||||
fmt.Println(t10 - t9)
|
||||
}
|
||||
|
||||
func main() {
|
||||
testViolenceCheck()
|
||||
}
|
||||
156
.example/encoding/gjson/issue#IZXU2.go
Normal file
156
.example/encoding/gjson/issue#IZXU2.go
Normal file
@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
)
|
||||
|
||||
type XinYanModel struct {
|
||||
Success bool `json:"success"`
|
||||
Data Data `json:"data"`
|
||||
ErrorCode interface{} `json:"errorCode"`
|
||||
ErrorMsg interface{} `json:"errorMsg"`
|
||||
}
|
||||
type ApplyReportDetail struct {
|
||||
ApplyScore string `json:"apply_score"`
|
||||
ApplyCredibility string `json:"apply_credibility"`
|
||||
QueryOrgCount string `json:"apply_query_org_count"`
|
||||
QueryFinanceCount string `json:"apply_query_finance_count"`
|
||||
QueryCashCount string `json:"apply_query_cash_count"`
|
||||
QuerySumCount string `json:"apply_query_sum_count"`
|
||||
LatestQueryTime string `json:"apply_latest_query_time"`
|
||||
LatestOneMonth string `json:"apply_latest_one_month"`
|
||||
LatestThreeMonth string `json:"apply_latest_three_month"`
|
||||
LatestSixMonth string `json:"apply_latest_six_month"`
|
||||
}
|
||||
type BehaviorReportDetail struct {
|
||||
LoansScore string `json:"behavior_report_detailloans_score"`
|
||||
LoansCredibility string `json:"behavior_report_detailloans_credibility"`
|
||||
LoansCount string `json:"behavior_report_detailloans_count"`
|
||||
LoansSettleCount string `json:"behavior_report_detailloans_settle_count"`
|
||||
LoansOverdueCount string `json:"behavior_report_detailloans_overdue_count"`
|
||||
LoansOrgCount string `json:"behavior_report_detailloans_org_count"`
|
||||
ConsfinOrgCount string `json:"behavior_report_detailconsfin_org_count"`
|
||||
LoansCashCount string `json:"behavior_report_detailloans_cash_count"`
|
||||
LatestOneMonth string `json:"behavior_report_detaillatest_one_month"`
|
||||
LatestThreeMonth string `json:"behavior_report_detaillatest_three_month"`
|
||||
LatestSixMonth string `json:"behavior_report_detaillatest_six_month"`
|
||||
HistorySucFee string `json:"behavior_report_detailhistory_suc_fee"`
|
||||
HistoryFailFee string `json:"behavior_report_detailhistory_fail_fee"`
|
||||
LatestOneMonthSuc string `json:"behavior_report_detaillatest_one_month_suc"`
|
||||
LatestOneMonthFail string `json:"behavior_report_detaillatest_one_month_fail"`
|
||||
LoansLongTime string `json:"behavior_report_detailloans_long_time"`
|
||||
LoansLatestTime string `json:"behavior_report_detailloans_latest_time"`
|
||||
}
|
||||
type CurrentReportDetail struct {
|
||||
LoansCreditLimit string `json:"current_report_detailloans_credit_limit"`
|
||||
LoansCredibility string `json:"current_report_detailloans_credibility"`
|
||||
LoansOrgCount string `json:"current_report_detailloans_org_count"`
|
||||
LoansProductCount string `json:"current_report_detailloans_product_count"`
|
||||
LoansMaxLimit string `json:"current_report_detailloans_max_limit"`
|
||||
LoansAvgLimit string `json:"current_report_detailloans_avg_limit"`
|
||||
ConsfinCreditLimit string `json:"current_report_detailconsfin_credit_limit"`
|
||||
ConsfinCredibility string `json:"current_report_detailconsfin_credibility"`
|
||||
ConsfinOrgCount string `json:"current_report_detailconsfin_org_count"`
|
||||
ConsfinProductCount string `json:"current_report_detailconsfin_product_count"`
|
||||
ConsfinMaxLimit string `json:"current_report_detailconsfin_max_limit"`
|
||||
ConsfinAvgLimit string `json:"current_report_detailconsfin_avg_limit"`
|
||||
}
|
||||
type ResultDetail struct {
|
||||
ApplyReportDetail ApplyReportDetail `json:"apply_report_detail"`
|
||||
BehaviorReportDetail BehaviorReportDetail `json:"behavior_report_detail"`
|
||||
CurrentReportDetail CurrentReportDetail `json:"current_report_detail"`
|
||||
}
|
||||
type Data struct {
|
||||
Code string `json:"code"`
|
||||
Desc string `json:"desc1"`
|
||||
TransID string `json:"trans_id"`
|
||||
TradeNo string `json:"trade_no"`
|
||||
Fee string `json:"fee"`
|
||||
IDNo string `json:"id_no"`
|
||||
IDName string `json:"id_name"`
|
||||
Versions string `json:"versions"`
|
||||
ResultDetail ResultDetail `json:"result_detail"`
|
||||
}
|
||||
|
||||
var data = `{
|
||||
"success": true,
|
||||
"data": {
|
||||
"code": "0",
|
||||
"desc": "查询成功",
|
||||
"trans_id": "14910304379231213",
|
||||
"trade_no": "201704011507240100057329",
|
||||
"fee": "Y",
|
||||
"id_no": "0783231bcc39f4957e99907e02ae401c",
|
||||
"id_name": "dd67a5943781369ddd7c594e231e9e70 ",
|
||||
"versions": "1.0.0",
|
||||
"result_detail":{
|
||||
"apply_report_detail": {
|
||||
"apply_score": "189",
|
||||
"apply_credibility": "84",
|
||||
"query_org_count": "7",
|
||||
"query_finance_count": "2",
|
||||
"query_cash_count": "2",
|
||||
"query_sum_count": "13",
|
||||
"latest_query_time": "2017-09-03",
|
||||
"latest_one_month": "1",
|
||||
"latest_three_month": "5",
|
||||
"latest_six_month": "12"
|
||||
},
|
||||
"behavior_report_detail": {
|
||||
"loans_score": "199",
|
||||
"loans_credibility": "90",
|
||||
"loans_count": "300",
|
||||
"loans_settle_count": "280",
|
||||
"loans_overdue_count": "20",
|
||||
"loans_org_count": "5",
|
||||
"consfin_org_count": "3",
|
||||
"loans_cash_count": "2",
|
||||
"latest_one_month": "3",
|
||||
"latest_three_month": "20",
|
||||
"latest_six_month": "23",
|
||||
"history_suc_fee": "30",
|
||||
"history_fail_fee": "25",
|
||||
"latest_one_month_suc": "5",
|
||||
"latest_one_month_fail": "20",
|
||||
"loans_long_time": "130",
|
||||
"loans_latest_time": "2017-09-16"
|
||||
},
|
||||
"current_report_detail": {
|
||||
"loans_credit_limit": "1400",
|
||||
"loans_credibility": "80",
|
||||
"loans_org_count": "7",
|
||||
"loans_product_count": "8",
|
||||
"loans_max_limit": "2000",
|
||||
"loans_avg_limit": "1000",
|
||||
"consfin_credit_limit": "1500",
|
||||
"consfin_credibility": "90",
|
||||
"consfin_org_count": "8",
|
||||
"consfin_product_count": "5",
|
||||
"consfin_max_limit": "5000",
|
||||
"consfin_avg_limit": "3000"
|
||||
}
|
||||
}
|
||||
},
|
||||
"errorCode": null,
|
||||
"errorMsg": null
|
||||
}`
|
||||
|
||||
func main() {
|
||||
struct1 := new(XinYanModel)
|
||||
err := json.Unmarshal([]byte(data), struct1)
|
||||
fmt.Println(err)
|
||||
fmt.Println(struct1)
|
||||
|
||||
fmt.Println()
|
||||
|
||||
struct2 := new(XinYanModel)
|
||||
j, err := gjson.DecodeToJson(data)
|
||||
fmt.Println(err)
|
||||
fmt.Println(j.Get("data.desc"))
|
||||
err = j.ToStruct(struct2)
|
||||
fmt.Println(err)
|
||||
fmt.Println(struct2)
|
||||
}
|
||||
Reference in New Issue
Block a user