Reputation: 413
I have a very simple json in a string:
{"username" : "a", "active" : 0}
I would like to convert this string into a json object of some sort. I just need to get the value of username using jsp. I have gson set up if that helps.
Thanks in advance for any help,
Upvotes: 1
Views: 9701
Reputation: 40887
Define a java class for storing your JSON, something like:
package com.onabai;
public class User {
public String username = "";
public int active = 0;
}
and then parsing the JSON from the JSP would be:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.gson.Gson" %>
<%@ page import="com.onabai.User" %>
<html>
<head>
<title>Powered by Zafu: OnaBai</title>
</head>
<body>
<%
String json = "{\"username\" : \"a\", \"active\" : 0}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
out.println("username:" + user.username);
%>
</body>
</html>
EDIT: If you only need the username
you might define User
as:
package com.onabai;
public class User {
public String username = "";
}
having access only to username
.
Upvotes: 3