如何从JSONObject检查值的类型?

编程入门 行业动态 更新时间:2024-10-25 02:23:40
本文介绍了如何从JSONObject检查值的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试获取存储在 JSONObject 中的值的类型。

I'm trying to get the type of the value stored in a JSONObject.

String jString = {"a": 1, "b": "str"}; JSONObject jObj = new JSONObject(jString);

是否可以获取存储在键中的值的类型a ; 类似 jObj.typeOf(a)= java.lang.Integer ?

Is it possible to get the type of the value stored at key "a"; something like jObj.typeOf("a") = java.lang.Integer?

推荐答案

您可以使用 get() 方法然后使用 instanceof 运算符检查对象的类型。这样的事情: -

You can get the object from the JSON using the get() method and then use the instanceof operator to check for the type of Object. Something like this:-

String jString = "{\"a\": 1, \"b\": \"str\"}"; JSONObject jObj = new JSONObject(jString); Object aObj = jObj.get("a"); if(aObj instanceof Integer){ System.out.println(aObj); }

更多推荐

如何从JSONObject检查值的类型?

本文发布于:2023-10-17 07:08:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   JSONObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!