Java 如何迭代JsonObject(gson)
我有一个JsonObjectJava 如何迭代JsonObject(gson),java,json,gson,json-deserialization,Java,Json,Gson,Json Deserialization,我有一个JsonObject JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"} 每个JsonObject都包含一个“id”条目,但其他键/值对的数量尚未确定,因此我想创建一个具有两个属性的对象: class myGenericObject { Map<String, Object> attributes; String id; } 我找到了这个解决方案 Map<String,
JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"}
每个JsonObject
都包含一个“id”
条目,但其他键/值对的数量尚未确定,因此我想创建一个具有两个属性的对象:
class myGenericObject {
Map<String, Object> attributes;
String id;
}
我找到了这个解决方案
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()));
}
如何获取普通值(4711
和“val1”
)
输入数据:
{
"id": 0815,
"a": "a string",
"b": 123.4,
"c": {
"a": 1,
"b": true,
"c": ["a", "b", "c"]
}
}
或
将“”替换为空白
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
if (! nonProperties.contains(entry.getKey())) {
properties.put(entry.getKey(), jsonObject.get(entry.getKey()).replace("\"",""));
}
}
Map attributes=newhashmap();
Set entrySet=jsonObject.entrySet();
for(Map.Entry:entrySet){
如果(!nonproperty.contains(entry.getKey())){
properties.put(entry.getKey(),jsonObject.get(entry.getKey()).replace(“\”,”);
}
}
< /代码> < p>我不太清楚为什么你要先做手动操作。GSON解码只会把那些缺省的键值对作为默认值(0,null)。然后你可以按你想要的方式处理。 你是如何创建你的jSONTRORM的?你的代码对我有用。考虑这个
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
...
...
...
try{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("keyInt", 2);
jsonObject.addProperty("keyString", "val1");
jsonObject.addProperty("id", "0123456");
System.out.println("json >>> "+jsonObject);
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()));
}
for(Map.Entry<String,Object> att : attributes.entrySet()){
System.out.println("key >>> "+att.getKey());
System.out.println("val >>> "+att.getValue());
}
}
catch (Exception ex){
System.out.println(ex);
}
import com.google.gson.JsonElement;
导入com.google.gson.JsonObject;
...
...
...
试一试{
JsonObject JsonObject=新的JsonObject();
jsonObject.addProperty(“keyInt”,2);
addProperty(“keyString”、“val1”);
jsonObject.addProperty(“id”,“0123456”);
System.out.println(“json>>>”+jsonObject);
Map attributes=newhashmap();
Set entrySet=jsonObject.entrySet();
for(Map.Entry:entrySet){
attributes.put(entry.getKey(),jsonObject.get(entry.getKey());
}
对于(Map.Entry att:attributes.entrySet()){
System.out.println(“key>>>”+att.getKey());
System.out.println(“val>>>”+att.getValue());
}
}
捕获(例外情况除外){
系统输出打印项次(ex);
}
现在我很想知道你是如何创建JSON的
您也可以试试这个(JSONObject)
import org.json.JSONObject;
...
...
...
试一试{
JSONObject JSONObject=新的JSONObject(“{\'keyInt\”:2,\'keyString\:\'val1\,\'id\:\'0123456\”);
System.out.println(“JSON::”+jsonObject.toString());
迭代器it=jsonObject.keys();
while(it.hasNext()){
String key=it.next();
System.out.println(“键::!!!>>>”+键);
Object value=jsonObject.get(key);
System.out.println(“值类型”+Value.getClass().getName());
}
}
捕获(例外情况除外){
系统输出打印项次(ex);
}
只需做以下更改
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()).getAsString());
}
Map attributes=newhashmap();
Set entrySet=jsonObject.entrySet();
for(Map.Entry:entrySet){
attributes.put(entry.getKey(),jsonObject.get(entry.getKey()).getAsString());
}
“getAsString”将为u发挥神奇的作用,您的映射值是s。每当您打印JsonElement
(例如,使用调试器)它的方法将被调用-由于JsonElement
有许多实现类,默认的toString
实现将值用引号括起来,以确保正确的JSON。要将值作为正常的、未包装的字符串
,只需调用:
以你的例子:
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()).getAsString());
}
Map attributes=newhashmap();
Set entrySet=jsonObject.entrySet();
for(Map.Entry:entrySet){
attributes.put(entry.getKey(),jsonObject.get(entry.getKey()).getAsString());
}
请注意,还有许多其他方法可以调用JsonElement
来生成其他类型。这只“解决”了“val1”的问题,而不是“4711”的问题。请使用entry.getValue().getAsString()'intsead'jsonObject.get(entry.getKey()).replace(“\”,“);是的,我可以剪切第一个和最后一个字符(“),但这将是一个悲哀的解决方案(或者不是?),我想用一个(或多或少通用的)类来处理所有传入的json(带有一个键“id”和一个额外的未知数目的键/值对),其中所有键/值对都是key!=“id“存储在哈希映射中。Atm我设法做到了,但每个值都被包装在“.嗯,我不创建任何JSON,我从数据库中获取它。你能粘贴你从数据库中获取的转储文件(到问题中)吗?
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
...
...
...
try{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("keyInt", 2);
jsonObject.addProperty("keyString", "val1");
jsonObject.addProperty("id", "0123456");
System.out.println("json >>> "+jsonObject);
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()));
}
for(Map.Entry<String,Object> att : attributes.entrySet()){
System.out.println("key >>> "+att.getKey());
System.out.println("val >>> "+att.getValue());
}
}
catch (Exception ex){
System.out.println(ex);
}
import org.json.JSONObject;
...
...
...
try{
JSONObject jsonObject = new JSONObject("{\"keyInt\":2,\"keyString\":\"val1\",\"id\":\"0123456\"}");
System.out.println("JSON :: "+jsonObject.toString());
Iterator<String> it = jsonObject.keys();
while( it.hasNext() ){
String key = it.next();
System.out.println("Key:: !!! >>> "+key);
Object value = jsonObject.get(key);
System.out.println("Value Type "+value.getClass().getName());
}
}
catch (Exception ex){
System.out.println(ex);
}
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()).getAsString());
}
JsonElement elem;
// ...
String value = elem.getAsString();
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()).getAsString());
}