File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Parse  JSON file with servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Parse  JSON file with servlet" Watch "Parse  JSON file with servlet" New topic
Author

Parse JSON file with servlet

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hello All,

I am trying to parse a JSON file inside a servlet. Can you tell me how do i get started. The sample of JSON file is as below:-

{ items: [
{
label: "Setting up the Environment for CI",
id: "1",
message: "Dojo is available under *either* the terms of the modified BSD " +
"license *or* the Academic Free License version",
}
]}

If i want to get the value of the message property in the JSON file, how do i get that? Also can you point to Servlet+JSON tutorials.

-Adi
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I recommend you don't try to do this yourself and use a java library that exists already for this. I've used the one from the site http://www.json.org/ and it would be more than useable in your situation.

Sean


I love this place!
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks Sean,

Can you get me started on this with a small example. I can pick from there.

-Adi
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61436
    
  67

An example is only possible if you pick one of the libraries. Personally, I've been using Google's Gson library.

By the way, most of these libraries will require strictly valid JSON as input; which your example is not.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30772
    
156

I've also been using GSON because it is so simple.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parse JSON file with servlet