Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

dictionary - Is there any convenient way to get JSON element without type assertion?

There is some inconvenience while processing JSON response from a web server.

For example, I don't know the data structure (and don't want to model it) of the JSON in advance, and just want to get the value from it!

So, for Python, I can just write

value = response["body"][4]["data"]["uid"]  //response is a dictionary

But for Golang, I need to do the assertion for every element!

value := response["body"].([]interface{})[4].(map[string]interface{})["data"].(map[string]interface{})["uid"]
//response is a map[string]interface{}

This is what I write in golang to get the value I need. Do you have any suggestion on it? It there any useful tips for this kind of case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...