Java 使用JSONReader或JSONObject/JSONArray解析JSON数据
我有一些JSON(如下所示),我试图解析整个JSON,每个对象都将是一个类的新实例,该类声明下面的变量。最好的方法是什么?我应该使用JSONReader还是使用JSONObject和JSONArray。我读了一些教程,问了一些一般性的问题,但是我还没有看到任何关于如何解析这样的数据的例子Java 使用JSONReader或JSONObject/JSONArray解析JSON数据,java,android,json,Java,Android,Json,我有一些JSON(如下所示),我试图解析整个JSON,每个对象都将是一个类的新实例,该类声明下面的变量。最好的方法是什么?我应该使用JSONReader还是使用JSONObject和JSONArray。我读了一些教程,问了一些一般性的问题,但是我还没有看到任何关于如何解析这样的数据的例子 { "id": 356, "hassubcategories": true, "subcategories": [ { "id": 3808,
{
"id": 356,
"hassubcategories": true,
"subcategories": [
{
"id": 3808,
"CategoryName": "Current Products",
"CategoryImage": null,
"hassubcategories": true,
"subcategories": [
{
"id": 4106,
"CategoryName": "Architectural",
"CategoryImage": "2637",
"hassubcategories": true,
"subcategories": [
{
"id": 391,
"CategoryName": "Flooring",
"CategoryImage": "2745",
"hassubcategories": false
}
]
}
]
},
{
"id": 3809,
"CategoryName": "Non-Current Products",
"CategoryImage": null,
"hassubcategories": true,
"subcategories": [
{
"id": 4107,
"CategoryName": "Desk",
"CategoryImage": "2638",
"hassubcategories": true,
"subcategories": [
{
"id": 392,
"CategoryName": "Wood",
"CategoryImage": "2746",
"hassubcategories": false
}
]
}
]
}
]
}
当您必须处理嵌套对象时,GSON是最简单的方法 像这样:
//after the fetched Json:
Gson gson = new Gson();
Event[] events = gson.fromJson(yourJson, Event[].class);
//somewhere nested in the class:
static class Event{
int id;
String categoryName;
String categoryImage;
boolean hassubcategories;
ArrayList<Event> subcategories;
}
//在获取的Json之后:
Gson Gson=新的Gson();
Event[]events=gson.fromJson(yourJson,Event[].class);
//嵌套在类中的某个位置:
静态类事件{
int-id;
字符串类别名称;
字符串分类图像;
布尔hassubcategories;
阵列列表子类别;
}
您可以查看本教程,或者如果我要这样做,我将把整个字符串解析为JSONObject
JSONObject obj = new JSONObject(str);
然后我看到你的子类别是一个JSONArray。所以我会像这样转换它
JSONArray arr = new JSONArray(obj.get("subcategories"));
有了它,您可以执行循环并实例化类对象
for(int i = 0; i < arr.length; i++)
JSONObject temp = arr.getJSONObject(i);
Category c = new Category();
c.setId(temp.get("id"));
for(int i=0;i
您发布的示例JSON数据似乎没有遵循JSON的数据结构。你需要完全按照Mustafa在第三篇文章中所教的方法构建数据。这是一个非常棒的教程。我遵循这些步骤,它真的很有效 只有当JSON数据的大小小于1MB时,才能使用JSON对象/JSON数组。否则你应该使用JSONReader。JSONReader实际上使用流式处理方法,而JSONObject和JSONArray最终会一次将所有数据加载到RAM上,这在json较大的情况下会导致OutOfMemoryException。这是使用Gson通过JSONReader对对象的arraylist建模的一个简单示例:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textviewtest);
Task task = new Task();
task.execute();
}
private class Task extends AsyncTask<Void, Void, ArrayList<Flower>> {
@Override
protected ArrayList<Flower> doInBackground(Void... params) {
ArrayList<Flower> arrayFlowers = new ArrayList<Flower>();
Flower[] f = null;
try {
String uri = "http://services.hanselandpetal.com/feeds/flowers.json";
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new InputStreamReader(con.getInputStream()));
f = gson.fromJson(reader, Flower[].class);
for (Flower flower : f) {
arrayFlowers.add(flower);
}
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
return arrayFlowers;
}
@Override
protected void onPostExecute(ArrayList<Flower> result) {
StringBuilder sb = new StringBuilder();
for (Flower flower : result) {
sb.append(flower.toString());
}
tv.setText(sb.toString());
}
}
这是我用来解析嵌套JSON对象中所有记录的代码 使用BSON时,它将值作为
对象返回。因此,为了使用与实际类型相关联的方法(int
、Document
、ArrayList
等),必须将返回值强制转换为预期的类型
// import java.util.ArrayList;
// import org.bson.Document;
Document root = Document.parse("{ \"id\" : 356, \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 3808, \"CategoryName\" : \"Current Products\", \"CategoryImage\" : \"image.img\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 4106, \"CategoryName\" : \"Architectural\", \"CategoryImage\" : \"2637\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 391, \"CategoryName\" : \"Flooring\", \"CategoryImage\" : \"2745\", \"hassubcategories\" : false }] }] }, { \"id\" : 3809, \"CategoryName\" : \"Non-Current Products\", \"CategoryImage\" : \"image.img\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 4107, \"CategoryName\" : \"Desk\", \"CategoryImage\" : \"2638\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 392, \"CategoryName\" : \"Wood\", \"CategoryImage\" : \"2746\", \"hassubcategories\" : false }] }] }] }");
System.out.println((root.get("id")));
System.out.println((root.get("hassubcategories")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(1)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(1)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(1)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(1)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));
只有当JSON数据的大小小于1MB时,您才能使用JSON对象/JSON数组
这些信息是在什么地方正式记录的?@aad编程一些线索来自于开发人员,他们曾面临某个问题,但无法从正式文件中找到解决方案:-)
// import java.util.ArrayList;
// import org.bson.Document;
Document root = Document.parse("{ \"id\" : 356, \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 3808, \"CategoryName\" : \"Current Products\", \"CategoryImage\" : \"image.img\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 4106, \"CategoryName\" : \"Architectural\", \"CategoryImage\" : \"2637\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 391, \"CategoryName\" : \"Flooring\", \"CategoryImage\" : \"2745\", \"hassubcategories\" : false }] }] }, { \"id\" : 3809, \"CategoryName\" : \"Non-Current Products\", \"CategoryImage\" : \"image.img\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 4107, \"CategoryName\" : \"Desk\", \"CategoryImage\" : \"2638\", \"hassubcategories\" : true, \"subcategories\" : [{ \"id\" : 392, \"CategoryName\" : \"Wood\", \"CategoryImage\" : \"2746\", \"hassubcategories\" : false }] }] }] }");
System.out.println((root.get("id")));
System.out.println((root.get("hassubcategories")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(0)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(1)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(1)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)root.get("subcategories")).get(1)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)root.get("subcategories")).get(1)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("hassubcategories")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryName")));
System.out.println(((String)((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("CategoryImage")));
System.out.println((((Document)((ArrayList)((Document)((ArrayList)((Document)((ArrayList)root.get("subcategories")).get(1)).get("subcategories")).get(0)).get("subcategories")).get(0)).get("hassubcategories")));