[英]How to iterate a JsonObject (gson)
I have a JsonObject eg我有一个 JsonObject 例如
JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"}
Every JsonObject
contains a "id"
entry, but the number of other key/value pairs is NOT determined, so I want to create create an object with 2 attributes:每个
JsonObject
包含一个"id"
条目,但其他键/值对的数量未确定,因此我想创建一个具有 2 个属性的对象:
class myGenericObject {
Map<String, Object> attributes;
String id;
}
So I want my attributes map to look like this:所以我希望我的属性映射看起来像这样:
"keyInt" -> 4711
"keyStr" -> "val1"
I found this solution我找到了这个解决方案
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()));
}
but the values are enclosed by ""
但值用
""
括起来
"keyInt" -> "4711"
"keyStr" -> ""val1""
How to get the plain values ( 4711
and "val1"
)?如何获得普通值(
4711
和"val1"
)?
Input data:输入数据:
{
"id": 0815,
"a": "a string",
"b": 123.4,
"c": {
"a": 1,
"b": true,
"c": ["a", "b", "c"]
}
}
or或者
{
"id": 4711,
"x": false,
"y": "y?",
}
replace "" with blank.将“”替换为空白。
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("\"",""));
}
}
How are you creating your JsonObject?你是如何创建你的 JsonObject 的? Your code works for me.
你的代码对我有用。 Consider this
考虑这个
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);
}
And it is working fine.它工作正常。 Now I am interested in knowing how you created that JSON of yours?
现在我有兴趣知道您是如何创建您的 JSON 的?
You can also try this (JSONObject)你也可以试试这个(JSONObject)
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);
}
Just make following changes...只需进行以下更改...
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());
}
"getAsString" will do the magic for u “getAsString” 会为你带来魔力
Your Map values are JsonElement
s.您的 Map 值是
JsonElement
。 Whenever you print a JsonElement
(eg using a debugger) its toString()
method will be called - and since a JsonElement
has many implementing classes the default toString
implementation wraps the value in quotes to ensure correct JSON.每当您打印
JsonElement
(例如使用调试器)时,它的toString()
方法将被调用 - 由于JsonElement
有许多实现类,因此默认的toString
实现将值包装在引号中以确保正确的 JSON。 To get the value as a normal, unwrapped String
, simply call getAsString()
:要获取正常的未包装
String
,只需调用getAsString()
:
JsonElement elem;
// ...
String value = elem.getAsString();
With your example:以你的例子:
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());
}
Note there are many other methods you can call on a JsonElement
to produce other types.请注意,您可以在
JsonElement
上调用许多其他方法来生成其他类型。
I am not quite sure why you want to do manual manipulation in the first place.我不太确定你为什么要首先进行手动操作。 GSON decode will simply leave those absent key/value pairs as default value (zero,null).
GSON 解码只会将那些不存在的键/值对保留为默认值(零,空)。 And then you can process as you want.
然后您可以根据需要进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.