从资源中读取JSON文件并获得JsonObject。

huangapple go评论70阅读模式
英文:

Read a JSON File from Resources with JsonObject

问题

以下是您要翻译的内容:

我想将Resources中的JSON文件分配给一个JsonObject的实例并进行解析。
请指导如何做?

MainActivicy:

boolean bool = true;
boolean bool2 = true;
String s = "";
input = getResources().openRawResource(R.raw.infojson);

JsonReader reader = new JsonReader(new InputStreamReader(input));

reader.beginObject();

while (bool == true) {
    String sv = "";
    sv = reader.nextName();
    s += sv;

    if (sv.equals("id") | sv.equals("num")) {
        s += " : ";
        s += (String.valueOf(reader.nextInt()));
    }
    if (sv.equals("name")) {
        s += " : ";
        s += reader.nextString();
    }
    s += "\t";
    bool = reader.hasNext();
}

JSON内容:

{
    "id": "1",
    "name": "E1",
    "num": 1111,
    "My": {
        "id": "2",
        "name": "E2",
        "num": 2222
    }
}
英文:

I want to assign the JSONfile inside the Resources to a instance from JsonObject and parse it.
Please guide how?

MainActivicy:

    boolean bool = true;
    boolean bool2=true;
    String s = "";
    input = getResources().openRawResource(R.raw.infojson);

    JsonReader reader = new JsonReader(new InputStreamReader(input));

    reader.beginObject();

    while (bool==true){


        String sv ="";
        sv=reader.nextName();
        s +=sv;

        if(sv.equals("id")| sv.equals("num")){
            s +=" : " ;
            s+=(String.valueOf(reader.nextInt()));
        }
        if (sv.equals("name")){
            s +=" : " ;
            s+=reader.nextString();
        }
        s+="\t";
        bool = reader.hasNext();
    }

JSON Content:

{
    "id":"1","name":"E1","num":1111,
    "My":{"id":"2","name":"E2","num":2222}
}

答案1

得分: 0

Create a model to parse your json like below:

创建一个模型以解析你的 json,如下所示:

class ResourseResponse {
    private String id;
    private String name;
    private String num;

    //getter-setter
}

Then parse your json from JsonReader and create your model.

然后从 JsonReader 解析你的 json 并创建你的模型。

JsonReader reader = new JsonReader(new InputStreamReader(input));
Gson gson = new Gson();

if (reader.peek() != JsonToken.BEGIN_OBJECT) {
    reader.beginArray();
    while (reader.hasNext()) {
        ResourseResponse response = gson.fromJson(reader, ResourseResponse.class);

        //Add to list
    }
    reader.endArray();
} else {
    ResourseResponse message = gson.fromJson(reader, ResourseResponse.class);
}
英文:

Create a model to parse your json like below:

class ResourseResponse {
    private String id;
    private String name;
    private String num;

    //getter-setter
}

Then parse your json from JsonReader and create your model.

JsonReader reader = new JsonReader(new InputStreamReader(input));
Gson gson = new Gson();

if (reader.peek() != JsonToken.BEGIN_OBJECT) {
    reader.beginArray();
    while (reader.hasNext()) {
        ResourseResponse response = gson.fromJson(reader, ResourseResponse.class);

        //Add to list
    }
    reader.endArray();
} else {
    ResourseResponse message = gson.fromJson(reader, ResourseResponse.class);
    
}

答案2

得分: 0

If the JSON content is not too big, you can easily create org.json.JSONObject and iterate its keys.

String jsonStr = "{\"id\":\"1\",\"name\":\"E1\",\"num\":1111,\"My\":{\"id\":\"2\",\"name\":\"E2\",\"num\":2222}}";
JSONObject json = new JSONObject(jsonStr);
json.keys().forEachRemaining(x -> {
    System.out.println(x.toString());
    try {
        if (json.get(x.toString()) instanceof JSONObject) {
            System.out.println(json.get(x.toString())); // here you can iterate the internal json object
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
});

Output is:
num
name
id
My
{"num":2222,"name":"E2","id":"2"}

英文:

If the JSON content is not too big, you can easily create org.json.JSONObject and iterate its keys.

String jsonStr = "{\"id\":\"1\",\"name\":\"E1\",\"num\":1111, \"My\":{\"id\":\"2\",\"name\":\"E2\",\"num\":2222} }"
JSONObject json = new JSONObject(jsonStr);
json.keys().forEachRemaining(x -> {
    System.out.println(x.toString());
    try {
        if (json.get(x.toString()) instanceof JSONObject) {
            System.out.println(json.get(x.toString())); / here you can iterate the internal json object
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
});

Output is:
num

name

id

My

{"num":2222,"name":"E2","id":"2"}

huangapple
  • 本文由 发表于 2020年1月7日 00:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定