如何从Java中查找JSON字符串中的指定名称及其值? [英] How to find specified name and its value in JSON-string from Java?
问题描述
{
name:John,
age:20,
address:某地址,
someobject:{
field:value
}
}
什么是最简单的(但仍然正确,即正则表达式是不可接受的)方式找到字段 age
及其值(或确定没有给定名称的字段)?
p.s。任何开放源代码库都可以。
第2页:请不要发布链接到库 - 这不是一个有用的答案。 '显示我的代码'(c)。 使用JSON库解析字符串并检索值。
p>以下非常基本的示例使用内置的 更高级的JSON库,例如 jackson , google-gson , json-io 或 genson ,允许您直接将JSON对象转换为Java对象。 Let's assume we have the next JSON string: What is the easiest (but still correct, i.e. regular expressions are not acceptable) way to find field p.s. any open-source libs are ok. p.s.2: please, don't post links to the libraries - it's not a useful answer. 'Show me the code'(c). Use a JSON library to parse the string and retrieve the value. The following very basic example uses the built-in JSON parser from Android. More advanced JSON libraries, such as jackson, google-gson, json-io or genson, allow you to convert JSON objects to Java objects directly. 这篇关于如何从Java中查找JSON字符串中的指定名称及其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
字符串jsonString ={\name \ :\John \,\age \:\20 \,\address \:\some address \};
JSONObject jsonObject = new JSONObject(jsonString);
int age = jsonObject.getInt(age);
{
"name" : "John",
"age" : "20",
"address" : "some address",
"someobject" : {
"field" : "value"
}
}
age
and its value (or determine that there's no field with given name)? String jsonString = "{ \"name\" : \"John\", \"age\" : \"20\", \"address\" : \"some address\" }";
JSONObject jsonObject = new JSONObject(jsonString);
int age = jsonObject.getInt("age");