java将json字符砖转get参数,将JSON字符串转换为JSON对象以获取值

I am getting a JSON string and want to print Name values on the console via JSP. Can someone suggest how to do it?

String AllCustomLockingCriterias = '{"TemplateArray":[{"Id":16,"Name":"Machine","type":"PM"},

{"Id":17,"Name":"Ethernet","type":"PM"},

{"Id":18,"Name":"Hard Disk","type":"PM"}]}';

Output I need:

Machine

Ethernet

Hard Disc

I want to start a loop and my output will be:

Machine

Ethernet

Hard Disc

解决方案use Gson jar package(produced by google.com) , FastJson(produced by alibaba.com) or jackson to serialize or deserialize the json string and the Class object.One jar package is enough.

use maven pom dependency/gradle config to add the gson to your project or add the gson jar into your lib folder directly,it is all up to you, maven is preferred.

define the Java Class field member,with the meta info from your json string,such as 'id','name','type'.The Java Class can be named 'Template'(do not forget to implement the java Serializable interface).

code example:

Gson gson = new Gson();

TypeToken typeToken = new TypeToken>() {};

Type type = typeToken.getType();

List templates = gson.fromJson(json, type);

return the templates list to the front jsp page within the jsp page scope.

if you user springMVC framework,you can add a model param to the method params,

@RequestMapping(value = "/test",method = RequestMethod.GET)

public String test(Model model){

model.addAttribute("templates",templates);

return "jspFileName";

}

for jsp site,you can use jsp EL Express to show the list

${template.name}

the last but the most easy method is ,you can pass the json string to the jsp page.on the other words,do not need to serialize the json string to class,just pass the string to the jsp with the model attribute provided by springMVC or even the basic Servlet.And then use the javascript method to handle the json string.for example,

var obj = JSON.parse(json);

var array = obj.TemplateArray;

array.foreach(function(item) {

console.log(item.name);

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值