在Java中反序列化JSON jqGrid结果

时间:2011-08-11 14:14:40

标签: java javascript json jqgrid

我有JSON结果:

[{"habilitado":true,"centroDeCustos.codigo":30,"centroDeCustos.nome":"INCUBATORIO OVOS - IBIPORÃ","_id_":10},
{"habilitado":true,"centroDeCustos.codigo":31,"centroDeCustos.nome":"FRIGORIFICO AVES - LONDRINA","_id_":11}, ... etc ]

如何反序列化Centro De Custos.codigo和centroDe Custos.nome

我的班级

@Deserializes({ "application/json", "json", "text/javascript" })
public class CustomJSONDeserealization implements Deserializer {
public Object[] deserialize(InputStream inputstream, ResourceMethod resourcemethod) {
    GsonBuilder gs = new GsonBuilder();
    Gson gson = gs.create();
    StreamToString sts = new StreamToString();
    try {

       BufferedReader br = new BufferedReader(new InputStreamReader(inputstream));

       JSONArray js;
       js = new JSONArray(sts.convertStreamToString(inputstream));
       List<Teste> rows = gson.fromJson(js.toString(), new TypeToken<List<Teste>>(){}.getType());           

    System.out.println(rows); // for test
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

class StreamToString {
    public String convertStreamToString(InputStream is) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        return sb.toString();
    }
}

    class Teste {
        public Boolean habilitado;
        public CentroDeCustos centroDeCustos; // Is other Class
    }

public class CentroDeCustos {
    private int codigo;
        private String nome;

        // implementation class
}

//我的javascript帖子

        $(document).ready(function(){

            $("#salvarccustos").click(function(){       
                var idToDataIndex = jQuery('#listaccustos').jqGrid('getGridParam','_index');

                var id;
                for (id in idToDataIndex) {
                    if (idToDataIndex.hasOwnProperty(id)) {
                        var mydata = $("#listaccustos").jqGrid('getGridParam','data');
                    }
                }

            var postData = JSON.stringify(mydata);  
            alert("JSON serialized jqGrid data:\n" + postData);
                $.ajax({
                    type: 'POST',
                    url: '/webdip/usuario/ccustos/salvar',
                    contentType: 'application/json',                    
                    dataType: 'json',                   
                    processData: false,
                    data: postData,
                    success: function(result) {
                        alert('sucesso');
                        $('#listaccustos').trigger('reloadGrid');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert('erro: ' + XMLHttpRequest.responseText);
                        return false;
                    }
                });
            });
        });

0 个答案:

没有答案