如何在Java中解析JSON对象:
我正在尝试在 java 中解析为 JSON 对象。我使用 dojo.xhrPost()
方法发布 json 键值对象,如下所示:
dojo.xhrPost({
url: /esturl/,
handleAs : "text",
content : {INST1 : {"0001" : "aa"},
INST2 : {"0002" : "bb"},
INST3 : {"0003" : "cc"}},
load : load(data),
error : error
});
现在我正在从请求上下文中读取此帖子数据:
Map<String, String[]> paramMap = requestContext.getParameterMap();
当我在循环中打印此数据时:
Iterator it = paramMap.entrySet().iterator();
while(it.hasnext()){
Map.entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey());
System.out.println(pairs.getkValue());
}
这会返回我:
INST1
[Ljava.lang.String;@1b4cef
INST2
[Ljava.lang.String;@5801d
同样,但我应该得到像
INST1 : {"0001" : "aa"}, INST2 : {"0002" : "bb"}, INST3 : {"0003" : "cc"}}, 任何指导将不胜感激。
更新
当我尝试使用
String[] X = requestContext.getParameters{"INST1"};
if (X != null){
System.out.println(X.length);
System.out.println(X[0]);
System.out.println(X[0].length);
}
then 获取一个参数的值时,我得到:
1
[object Object]
[object Object]
Q 现在如何获取对象的实际值,如 INST1 : {"0001" : “aa”}
而不是 [object Object] ?
I am trying to parse to JSON object in java. I am posting json key value object using dojo.xhrPost()
method as shown below:
dojo.xhrPost({
url: /esturl/,
handleAs : "text",
content : {INST1 : {"0001" : "aa"},
INST2 : {"0002" : "bb"},
INST3 : {"0003" : "cc"}},
load : load(data),
error : error
});
Now I am reading this post data from request context:
Map<String, String[]> paramMap = requestContext.getParameterMap();
when I am printing this in loop:
Iterator it = paramMap.entrySet().iterator();
while(it.hasnext()){
Map.entry pairs = (Map.Entry) it.next();
System.out.println(pairs.getKey());
System.out.println(pairs.getkValue());
}
this returns me :
INST1
[Ljava.lang.String;@1b4cef
INST2
[Ljava.lang.String;@5801d
like wise, but I should be getting values like
INST1 : {"0001" : "aa"}, INST2 : {"0002" : "bb"}, INST3 : {"0003" : "cc"}},
any guidance would be highly appreciated.
Update
When I try to get value of one parameter using
String[] X = requestContext.getParameters{"INST1"};
if (X != null){
System.out.println(X.length);
System.out.println(X[0]);
System.out.println(X[0].length);
}
then am getting :
1
[object Object]
[object Object]
Q Now how can I get actual values of Object like INST1 : {"0001" : "aa"}
instead of [object Object] ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此修改后的 JSONObject 类,将原始 JSON 代码传递给构造函数或创建(新对象并稍后分配 JSON 内容..)并使用内置 get 方法。 此处查看完整的 javadoc。
you can use this modified JSONObject class, pass the raw JSON code through the constructor or create (new object and assign JSON content later..) and use built-in get methods. view full javadoc here.
正如 dojo 文档此处中给出的,您设置的对象将被读取为名称1=值1。因此,在您的情况下,变量可能会像 INST1=0001,INST1=aa 一样传递。
您可以尝试在这些行上创建“内容”的语法 -
INST1 : '{"0001" : "aa"}'
或INST1 : "{\"0001\" : \"aa\"}"
以便 INST1 有 1 个明确的值as given in the dojo documentation here the object which you set up will be read as name1=value1. So may be in your case the variables are being passed like INST1=0001,INST1=aa.
You can try to make the syntax of 'content' on these lines -
INST1 : '{"0001" : "aa"}'
orINST1 : "{\"0001\" : \"aa\"}"
so that INST1 has 1 unambiguous value