简体   繁体   English

如何在Java中解析JSON对象:

[英]How to parse JSON object in Java:

I am trying to parse to JSON object in java. 我正在尝试解析为Java中的JSON对象。 I am posting json key value object using dojo.xhrPost() method as shown below: 我正在使用dojo.xhrPost()方法发布json键值对象,如下所示:

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. INST1 : {"0001" : "aa"}, INST2 : {"0002" : "bb"}, INST3 : {"0003" : "cc"}},任何指导将不胜感激。

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] ? 问:现在如何获取INST1 : {"0001" : "aa"}类的Object的实际值,而不是[object Object]?

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. 您可以使用此修改后的JSONObject类 ,通过构造函数传递原始JSON代码,也可以创建(创建新对象并稍后分配JSON内容。)并使用内置的get方法。 view full javadoc here . 在此处查看完整的javadoc。

as given in the dojo documentation here the object which you set up will be read as name1=value1. 作为道场文档中给出这里你设置对象将被读作名1 =值。 So may be in your case the variables are being passed like INST1=0001,INST1=aa. 因此,在您的情况下,可能像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 您可以尝试在以下INST1 : '{"0001" : "aa"}'行中使用'content'的语法INST1 : '{"0001" : "aa"}'INST1 : "{\\"0001\\" : \\"aa\\"}"以便INST1具有1个明确的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM