[英]How to iterate a JsonObject (gson)
我有一個 JsonObject 例如
JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"}
每個JsonObject
包含一個"id"
條目,但其他鍵/值對的數量未確定,因此我想創建一個具有 2 個屬性的對象:
class myGenericObject {
Map<String, Object> attributes;
String id;
}
所以我希望我的屬性映射看起來像這樣:
"keyInt" -> 4711
"keyStr" -> "val1"
我找到了這個解決方案
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()));
}
但值用""
括起來
"keyInt" -> "4711"
"keyStr" -> ""val1""
如何獲得普通值( 4711
和"val1"
)?
輸入數據:
{
"id": 0815,
"a": "a string",
"b": 123.4,
"c": {
"a": 1,
"b": true,
"c": ["a", "b", "c"]
}
}
或者
{
"id": 4711,
"x": false,
"y": "y?",
}
將“”替換為空白。
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("\"",""));
}
}
你是如何創建你的 JsonObject 的? 你的代碼對我有用。 考慮這個
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);
}
它工作正常。 現在我有興趣知道您是如何創建您的 JSON 的?
你也可以試試這個(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);
}
只需進行以下更改...
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” 會為你帶來魔力
您的 Map 值是JsonElement
。 每當您打印JsonElement
(例如使用調試器)時,它的toString()
方法將被調用 - 由於JsonElement
有許多實現類,因此默認的toString
實現將值包裝在引號中以確保正確的 JSON。 要獲取正常的未包裝String
,只需調用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());
}
請注意,您可以在JsonElement
上調用許多其他方法來生成其他類型。
我不太確定你為什么要首先進行手動操作。 GSON 解碼只會將那些不存在的鍵/值對保留為默認值(零,空)。 然后您可以根據需要進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.