0

I need help with this JSON parser. I have one container with comments which i want to get. This is the JSON string-code that i have: http://www.jsoneditoronline.org/?id=b73dc60acd235f60a647c02ed3e068db I need to get the atributes from each comment of the "resources" field (content, id, creator). I have tried this, between another things, and i cant obtain that i want.

public class CommentsParser {

    public List<Comment> readJsonStream(InputStream in) throws IOException {
        // Nueva instancia JsonReader
        System.out.println("Longitud del inputStream para comentarios: "+ in.toString().length());
        //Vemos lo que ha leido del inputStream
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        String linea = writer.toString();
        System.out.println("El is contiene: "+linea); // Lo que recibe en el inputStream
        //Vemos lo que ha leido del reader.
        System.out.println("El reader contiene: "+ reader.toString());
        try {
            // Leer Array
            return leeArrayComentarios(reader);
        } finally {
            reader.close();
        }

    }
    public List<Comment> leeArrayComentarios(JsonReader reader) throws IOException {
        // Lista temporal 
        List<Comment> listaComentarios = new ArrayList<Comment>();

        listaComentarios = formatComentario(reader);

        return listaComentarios;

    }

    public List<Comment> formatComentario(JsonReader reader) throws IOException {

        List<Comment> listaComentarios = new ArrayList<Comment>();
        String situacion;
        Comment comentario = new Comment();
        //System.out.println("Estamos en: "+ reader.nextString());
        //System.out.println("Estamos en: "+ reader.nextName());

        //reader.beginObject();
        while (reader.hasNext()) {
            String campo = reader.nextName();
            System.out.println("El objeto es:"+campo);
            switch (campo) { //Cuando intenta parsear no encuentra estos case y entonces devuelve a null el usuario.
                /*case "content":
                    Name nombre = new Name();
                    comentario.setContent(reader.nextString());
                    break;
                case "isRecomment":
                    comentario.setIsRecomment(reader.nextBoolean());
                    break;
                case "creator":
                    ActiveElement creador = new ActiveElement();
                    creador.setId(reader.nextLong());
                    comentario.setCreator(creador);*/
            case "_embebed":
                situacion = reader.nextName(); //Deberiamos pasar a resources
                //Una vez en resources tenemos una array de comentarios.

                System.out.println("Estamos en: "+ reader.nextString());
                reader.beginArray();
                while (reader.hasNext()) {
                    // Leer objeto
                    System.out.println("Dentro While.Estamos en: "+ reader.nextName());
                }
                default:
                    reader.skipValue();
                    break;
            }
        }
        reader.endObject();
        return listaComentarios;
    }

}

Edit 1 - Applying Gson:

public CommentContainer jsonToJava(String json){
        CommentContainer contenedor = new CommentContainer();
        Gson gson = new Gson();
        contenedor = gson.fromJson(json, CommentContainer.class);
        return contenedor;
    }

With this way, the CommentContainer i return is empty. The Json string is wellformed, as you can see in the URL of JsonEditor.

I dont want the solution, only the way with i can get these atributes for save them into an object. Thanks a lot.

Aszur
  • 1
  • 4

2 Answers2

0

You need an object that matches the JSON you are receiving. Then you just serialize it into that object:

var serializer = new JavaScriptSerializer();
YourDataObject dataObject = serializer.Deserialize<YourDataObject>(JsonData); //JsonData is your json string that comes from the API call to get it

Once you do that you can just use the properties of that object like so:

var resources = dataObject.resources;
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
0

Use GSON library https://github.com/google/gson . Refer http://www.studytrails.com/java/json/java-google-json-parse-json-to-java.jsp .

http://www.jsonschema2pojo.org/ use this to create a java class for the json string

manjusg
  • 2,275
  • 1
  • 22
  • 31
  • thanks, i did it -> Posted at Edit 1. However, the commentContainer is empty with that code... Why? Is not this way? – Aszur Jul 21 '15 at 15:57
  • there is another library that allows parse hal+json string? Any idea for it? – Aszur Jul 23 '15 at 16:04