开发者

Decoding JSON with unknown structure

I'm looking to get the structure of a JSON object without the predefined knowledge of what it may contain. To elaborate, I coul开发者_如何学JAVAd be given

[{"x":0,"y":0.4991088274400681,"z":7.489443555361306}, {"x":0,"y":0.7991088274400681,"z":7.489343555361306},{"x":0,"y":0.5991088274400681,"z":7.482445555361306}]

or even something crazy like:

'AL': {name:'Alabama', landarea:131426, waterarea:4338, population:[3525342,4243844]},
'AK': {name:'Alaska', landarea:1418400, waterarea:234324, population:[43230943,43230943]}

In other words, I have no idea what kind of JSON I'm going to get, but I'd like to know that there's x, y and z in the first one with an associated value, or AL, AK and name, landarea, waterarea, and an array of populations in the second.

This is sort of like getting the column headers in excel. I know that there will a repetitive structure, though.

Is the only way to parse this by using JSON_decode and then doing typeOf examinations to see if what I have is an Object/Array/String? It seems rather clunky.


A browser's native JSON decoding function can handle any well-formed JSON. What you want is a function that can understand the decoded JSON. That is not possible, at least not when you have absolutely no prior knowledge of, or a way of handling, the data. However, if all you want to know is the data type for each component, then typeof will work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜