如何在Java中解析JSON对象:

发布于 2024-09-28 02:18:45 字数 1343 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2024-10-05 02:18:45

您可以使用此修改后的 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.

幽梦紫曦~ 2024-10-05 02:18:45

正如 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"}' or
INST1 : "{\"0001\" : \"aa\"}" so that INST1 has 1 unambiguous value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文