我正在尝试“创建” JSONObject。现在,我正在使用JSON-Simple,并且正在尝试执行类似的操作(很抱歉,如果在此示例JSON文件中输入了任何错字)
{
"valuedata": {
"period": 1,
"icon": "pretty"
}
}
现在,我在寻找如何通过Java将valuedata写入JSON文件时遇到问题,我尝试做的是:
Map<String, String> t = new HashMap<String, String>();
t.put("Testing", "testing");
JSONObject jsonObject = new JSONObject(t);
但这只是做到了
{
"Testing": "testing"
}
您想要做的是将另一个JSONObject放在JSONObject“ jsonObject”内,更确切地说是在“ valuedata”字段中。你可以那样做...
// Create empty JSONObect here: "{}"
JSONObject jsonObject = new JSONObject();
// Create another empty JSONObect here: "{}"
JSONObject myValueData = new JSONObject();
// Now put the 2nd JSONObject into the field "valuedata" of the first:
// { "valuedata" : {} }
jsonObject.put("valuedata", myValueData);
// And now add all your fields for your 2nd JSONObject, for example period:
// { "valuedata" : { "period" : 1} }
myValueData.put("period", 1);
// etc.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句