libgdx Json解析 [英] libgdx Json parsing

查看:110
本文介绍了libgdx Json解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将json中的所有"id"值都放入结果"数组中.

hi I'm trying to get all 'id' value from my json into my 'results' array.

我不太了解libgdx的json类如何工作,但我知道json本身如何工作.

I didn't really understood how the json class of libgdx works, but I know how json works itself.

这是json : http://pastebin.com/qu71EnMx

这是我的代码:

        Array<Integer> results = new Array<Integer>();    

        Json jsonObject = new Json(OutputType.json);
        JsonReader jsonReader = new JsonReader();
        JsonValue jv = null;
        JsonValue jv_array = null;
        //
        try {
            String str = jsonObject.toJson(jsonString);
            jv = jsonReader.parse(str);
        } catch (SerializationException e) {
            //show error
        }
        //
        try {
            jv_array = jv.get("table");
        } catch (SerializationException e) {
            //show error
        }
        //
        for (int i = 0; i < jv_array.size; i++) {
            //
            try {

                jv_array.get(i).get("name").asString();

                results.add(new sic_PlayerInfos(
                        jv_array.get(i).get("id").asInt()
                        ));
            } catch (SerializationException e) {
                //show error
            }
        }

这是我得到的错误:jv_array.size上的'Nullpointer'

Here is the error I get : 'Nullpointer' on jv_array.size

推荐答案

以这种方式进行操作将导致代码非常笨拙,无法维护.您的JSON文件看起来非常简单,但是如果您自己解析整个JSON文件,则代码将很糟糕.试想一下,如果您拥有的不仅仅是id,它将是什么样子.

Doing it this way will result in a very hacky, not maintainable code. Your JSON file looks very simple but your code is terrible if you parse the whole JSON file yourself. Just imagine how it will look like if you are having more than an id, which is probably going to happen.

更干净的方法是面向对象的.创建一个类似于JSON文件结构的对象结构.在您的情况下,可能如下所示:

The much more clean way is object oriented. Create an object structure, which resembles the structure of your JSON file. In your case this might look like the following:

public class Data {

    public Array<TableEntry> table;

}

public class TableEntry {

    public int id;

}

现在,您可以使用libgdx轻松反序列化JSON,而无需任何自定义序列化程序,因为libgdx使用反射来处理大多数标准情况.

Now you can easily deserialize the JSON with libgdx without any custom serializers, because libgdx uses reflection to handle most standard cases.

Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(OutputType.json);

// I'm using your file as a String here, but you can supply the file as well
Data data = json.fromJson(Data.class, "{\"table\": [{\"id\": 1},{\"id\": 2},{\"id\": 3},{\"id\": 4}]}");

现在您已经有了一个普通的旧Java对象(PO​​JO),其中包含您需要的所有信息,并且您可以根据需要对其进行处理.

Now you've got a plain old java object (POJO) which contains all the information you need and you can process it however you want.

Array<Integer> results = new Array<Integer>();
for (TableEntry entry : data.table) {
    results.add(entry.id);
}

完成.非常干净的代码,易于扩展.

Done. Very clean code and easily extendable.

这篇关于libgdx Json解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆