Json + java + probelmas

3 respostas
R

Tenho o json abaixo:

{pedido:[{
        "campo1":"valor1",
       "campo2":"valor2",
           item:[{
                 "item_campo1":"iteam_valo1"
             }]
    }]}

Como faço para ler corretamente os valores sem ter perda de informações?

3 Respostas

aeciovc

perda de informações??

o que está acontecendo??

você consegue recuperar o Objeto normalmente.

daveiga

Vi a mesma pergunta no Stack Overflow, utilizando o http://code.google.com/p/google-gson/, talvez te ajude:

package com.stackoverflow.q1688099;

import java.util.List;
import com.google.gson.Gson;

public class Test {

    public static void main(String... args) throws Exception {
        String json = 
            "{"
                + "'title': 'Computing and Information systems',"
                + "'id' : 1,"
                + "'children' : 'true',"
                + "'groups' : [{"
                    + "'title' : 'Level one CIS',"
                    + "'id' : 2,"
                    + "'children' : 'true',"
                    + "'groups' : [{"
                        + "'title' : 'Intro To Computing and Internet',"
                        + "'id' : 3,"
                        + "'children': 'false',"
                        + "'groups':[]"
                    + "}]" 
                + "}]"
            + "}";

        // Now do the magic.
        Data data = new Gson().fromJson(json, Data.class);

        // Show it.
        System.out.println(data);
    }

}

class Data {
    private String title;
    private Long id;
    private Boolean children;
    private List<Data> groups;

    public String getTitle() { return title; }
    public Long getId() { return id; }
    public Boolean getChildren() { return children; }
    public List<Data> getGroups() { return groups; }

    public void setTitle(String title) { this.title = title; }
    public void setId(Long id) { this.id = id; }
    public void setChildren(Boolean children) { this.children = children; }
    public void setGroups(List<Data> groups) { this.groups = groups; }

    public String toString() {
        return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
    }
}

http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182

R

não eu não estou conseguindo. Como eu faria isso?

Criado 28 de abril de 2011
Ultima resposta 28 de abr. de 2011
Respostas 3
Participantes 3