Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

多层级JSON解析失败 #1234

Closed
zuozhehao opened this issue Apr 29, 2021 · 1 comment
Closed

多层级JSON解析失败 #1234

zuozhehao opened this issue Apr 29, 2021 · 1 comment
Labels

Comments

@zuozhehao
Copy link

zuozhehao commented Apr 29, 2021

1. What version of Go and system type/arch are you using?

go version go1.16.3 windows/amd64

2. What version of GoFrame are you using?

v1.15.6

3. Can this issue be reproduced with the latest release?

4. What did you do?

	xml := `<?xml version="1.0" encoding="UTF-8"?>
<result>
<request>
<response type="json"><![CDATA[{"data":[{"regionCode":"110000","regionName":"\u5317\u4EAC"}]}]]></response>
</request>
<request>
<response type="json"><![CDATA[{"data":{"data":[{"storageCode":"001","storageName":"\u5317\u4EAC"}]}}]]></response>
</request>
</result>`

	a, _ := gparser.LoadContent(xml, true)
	b := a.MustToJson()
	c, _ := gjson.DecodeToJson(b)

	t.Log(1, c.GetString("."))
	t.Log(2,c.GetString( "result.request.0.response.#text"))
	t.Log(3,c.GetString( "result.request.0.response.#text.data"))

	d := c.GetJson( "result.request.0.response.#text")
	t.Log(4, d.GetString("data"))

5. What did you expect to see?

之前版本可以获取到的

    json_test.go:25: 1 {"result":{"request":[{"response":{"#text":"{\"data\":[{\"regionCode\":\"110000\",\"regionName\":\"\\u5317\\u4EAC\"}]}","-type":"json"}},{"response":{"#text":"{\"data\":{\"data\":[{\"storageCode\":\"001\",\"storageName\":\"\\u5317\\u4EAC\"}]}}","-type":"json"}}]}}
    json_test.go:26: 2 {"data":[{"regionCode":"110000","regionName":"\u5317\u4EAC"}]}
    json_test.go:27: 3 
    json_test.go:30: 4 [{"regionCode":"110000","regionName":"北京"}]

6. What did you see instead?

@zuozhehao zuozhehao changed the title JSON 解析 JSON 解析被转义 Apr 29, 2021
@zuozhehao zuozhehao changed the title JSON 解析被转义 多层级JSON解析失败 May 14, 2021
@danvinhe
Copy link
Contributor

我试了1.14.x 1.15.x 1.16.x,CDATA里的字符串都不会直接解析未json对象。你需要先把其解析为json对象,然后再获取值,就像你的测试用例那样

	d := c.GetJson( "result.request.0.response.#text")
	t.Log(4, d.GetString("data"))

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants