0

JSON:

{"attribute1":11, "attribute2":"string atribute"}

I want to detect what kind of type are attribute1 and attribute2:

  • attribute1 is integer
  • attribute2 is string

jsonObject.getAttributeType("attribute2"); // should output: string/integer/boolean.

It was very easy to achieve in PHP or OBJC. Suggestions?

4

1 回答 1

2

(我假设该org.json软件包的 Android 与您可以在 json.org 网站上找到的相同......这里。)

a 上唯一JSONObject可以为您提供潜在价值的方法......而不是强制它......是JSONObject.get(name). 如果name已知,则结果是在内部表示该值的对象。我没有对代码进行全面的拖网,但我认为它只能是以下类型之一:

  Boolean, Long, Double, String, JSONArray, JSONObject

您应该能够使用instanceof.


但应该问问自己这是否是正确的做法。通过 API 处理 JSON 对象属性的常规JSONObject方法是使用将它们强制为您期望的类型的方法。在大多数情况下,数字发送为42or42.0"42"... 并不重要,如果意图易于确定,最好不要挑剔。

于 2013-07-09T22:57:05.963 回答