Quantcast
Channel: Is there any convenient way to get JSON element without type assertion? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images