英文:
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"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论