奇怪的“nameValuePairs”键在使用Gson时出现 [英] Strange "nameValuePairs" key appear when using Gson
问题描述
我试图从它的字段中重建一个 Object
(我将字段作为JSONObject),如下所示:
JSONObject jObj = new JSONObject();
JSONObject jObj1 = new JSONObject();
JSONObject jObj2 = new JSONObject();
JSONObject jObj21 = new JSONObject();
JSONObject jObj22 = new JSONObject();
jObj1.put(jObj11,value11);
jObj1.put(jObj12,value12);
jObj21.put(jObj211,value211); // level 2
jObj21.put(jObj212,value212);
jObj21.put(jObj213,value213);
jObj22.put(jObj221,value221);
jObj22.put(jObj222,value222);
jObj22.put(jObj223,value223);
jObj2.put(jObj21,jObj21); // level 1
jObj2.put(jObj22,jObj22);
jObj.put(jObj1,jObj1); // level 0
jObj.put(jObj2,jObj2);
我使用这些行从 Obeject $ c $获取Json c>
GsonBuilder builder = new GsonBuilder();
Gson gSon = builder.create();
gSon.toJSon(jObj);
问题是当我解析主要对象
(jObj)与Gson,我发现一个额外的键名为nameValuePairs
。那么,为什么我得到这个关键?
注意:
jObj.toString();
,则此键将消失。 如果我这样做: jObj.opt(nameValuePairs);
我有 Null 作为结果(就像没有名为nameValuePairs的键)。 $ b
这是我的实际结果:
这就是我所期望的:
<我找到了类似于我的问题,但它没有帮助。
是否有人有解决方法/解决方法或可以解释此密钥的来源?
谢谢。
尝试使用Gson的 JsonObject
而不是 JSONObject
像这样:
JsonObject jObj = new JsonObject();
JsonObject jObj1 = new JsonObject();
JsonObject jObj2 = new JsonObject();
JsonObject jObj21 = new JsonObject();
JsonObject jObj22 = new JsonObject();
jObj1.addProperty(jObj11,value11);
jObj1.addProperty(jObj12,value12);
jObj21.addProperty(jObj211,value211); // level 2
jObj21.addProperty(jObj212,value212);
jObj21.addProperty(jObj213,value213);
jObj22.addProperty(jObj221,value221);
jObj22.addProperty(jObj222,value222);
jObj22.addProperty(jObj223,value223);
jObj2.add(jObj21,jObj21); // level 1
jObj2.add(jObj22,jObj22);
jObj.add(jObj1,jObj1); // level 0
jObj.add(jObj2,jObj2);
String json = new Gson()。toJson(jObj);
I am trying to rebuild an Object
from its fields( i get the fields as a JSONObject), something like this:
JSONObject jObj = new JSONObject();
JSONObject jObj1 = new JSONObject();
JSONObject jObj2 = new JSONObject();
JSONObject jObj21 = new JSONObject();
JSONObject jObj22 = new JSONObject();
jObj1.put("jObj11", "value11");
jObj1.put("jObj12", "value12");
jObj21.put("jObj211", "value211"); // level 2
jObj21.put("jObj212", "value212");
jObj21.put("jObj213", "value213");
jObj22.put("jObj221", "value221");
jObj22.put("jObj222", "value222");
jObj22.put("jObj223", "value223");
jObj2.put("jObj21", jObj21); // level 1
jObj2.put("jObj22", jObj22);
jObj.put("jObj1", jObj1); // level 0
jObj.put("jObj2", jObj2);
I use those lines to get Json from an Obeject
GsonBuilder builder = new GsonBuilder();
Gson gSon = builder.create();
gSon.toJSon(jObj);
The Problem is when i parse the main Object
( jObj ) with Gson, i found an extra key named "nameValuePairs"
. So why i get this key?
Notice:
- If i do :
jObj.toString();
on Log, this key disappear. - If i do :
jObj.opt("nameValuePairs");
i have Null as result (like there is no key named "nameValuePairs").
This is my actual result:
And this is what i expect to have:
I found something similar to my problem, but it doesn't help.
Is there someone who have a solution/workaround or can explain me the origin of this key?
Thanks.
Try to use Gson's JsonObject
instead of JSONObject
like this:
JsonObject jObj = new JsonObject();
JsonObject jObj1 = new JsonObject();
JsonObject jObj2 = new JsonObject();
JsonObject jObj21 = new JsonObject();
JsonObject jObj22 = new JsonObject();
jObj1.addProperty("jObj11", "value11");
jObj1.addProperty("jObj12", "value12");
jObj21.addProperty("jObj211", "value211"); // level 2
jObj21.addProperty("jObj212", "value212");
jObj21.addProperty("jObj213", "value213");
jObj22.addProperty("jObj221", "value221");
jObj22.addProperty("jObj222", "value222");
jObj22.addProperty("jObj223", "value223");
jObj2.add("jObj21", jObj21); // level 1
jObj2.add("jObj22", jObj22);
jObj.add("jObj1", jObj1); // level 0
jObj.add("jObj2", jObj2);
String json = new Gson().toJson(jObj);
这篇关于奇怪的“nameValuePairs”键在使用Gson时出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!