2

How can I parse this JSON string to create collection object in servlet

{"title":["1","2"],"amount":["1","3"]}

inner class in my servlet

public class Data {
    private List<String> title;
    private List<String> amount;
  //getters and setters
}

parsing json

Gson gson = new Gson();
String param=request.getParameter("info");
Data data = gson.fromJson(param, Data.class);
List<String> a=data.getTitle();
 if(a==null){p("a null");}else{p("a not null");} //here a is null, prints "a null"

here is the jsfiddle of how I am creating the json string http://jsfiddle.net/testtracker/XDNLp/

client side in form submit function

var dataString=JSON.stringify($(this).serializeObject());
$.ajax({
    type: "POST",
    url: URL,
    data: {"info":JSON.stringify($(this).serializeObject())},
    success: function(data){

    }
  });

This is what I have till now. am I on correct way? what next should I do to System.print them?

18
  • Yes you're on right way. I can't understand what is the problem? Nov 30, 2012 at 15:38
  • Hmm toString() method in Data override and print... Nov 30, 2012 at 15:40
  • problem is when I call getter method and System.print, null pointer exception occures Nov 30, 2012 at 15:41
  • Can you add all code. (also the code that produce exception) Nov 30, 2012 at 15:42
  • 1
    why don't you print the param on the server so you can see what you actually got, not what you're doing on the client side.
    – Kevin
    Nov 30, 2012 at 15:49

2 Answers 2

3

When I am unable to solve something, I write the smallest possible program to verify my understanding is correct. In your case, I came up with this:

import java.util.List;
import com.google.gson.Gson;
public class GsonTest {

public static class Data {
    private List<String> title;
    public List<String> getTitle() {return title;}
    public Data() {}
}

public static void main (String [] args) {
    Gson gson = new Gson();
    Data data = gson.fromJson("{\"title\":[\"1\",\"2\"]}", Data.class);
    System.out.println(data.getTitle());
} 
}

Compiled, and ran, and it outputs:

["1", "2"]

So I still believe that the input that the servlet receives, is not correct (or you have not provided an accurate description of your existing code). Please compare the example above, against your real code.

2
  • Yes this example works, thanks for this I will try to make my code by comparing it with this. thanks Nov 30, 2012 at 16:33
  • this example really helped me, as I was working on json first time, I was looking for complete general solution, searched on SO but those questions were specific to their issues, anyways thanks +1 and and aceepting this Nov 30, 2012 at 17:13
1

try

public class Data {
    private ArrayList<String> title;
    private ArrayList<String> amount;
  //getters and setters
}

List is a abstract class (So GSON doesn't know how to create it)

5
  • You can't use abstract class as List in object Data. Because GSON can't get instance of abstract class, it is impossible. Nov 30, 2012 at 16:09
  • but now I have ArrayList<String> Nov 30, 2012 at 16:10
  • Can you write getter for title? And maybe you have constructor? Nov 30, 2012 at 16:14
  • this is my getter method in Data public ArrayList<String> getTitle() { return title; } , No constructor in Data Nov 30, 2012 at 16:16
  • this is my setter method public void setTitle(ArrayList<String> title) { this.title = title; } form field names has to be same as the variables in setter method? Nov 30, 2012 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.