我有一些JSON(如下所示),我试图解析整个JSON,每个对象都是一个声明下面变量的类的新实例。做这个的最好方式是什么?我应该使用JSONReader还是使用JSONObject和JSONArray。我一直在阅读一些教程并询问一些一般性问题,但我还没有看到任何如何解析这样的数据的例子。
{
"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
}
]
}
]
}
]
}
答案 0 :(得分:9)
仅当json数据的大小小于1MB时,才能使用JSON Object / JSON Array。否则你应该使用JSONReader。 JSONReader实际上使用流式方法,而JSONObject和JSONArray最终会立即加载RAM上的所有数据,如果json更大,会导致OutOfMemoryException。
答案 1 :(得分:5)
当您必须使用嵌套对象时,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;
}
您可以查看本教程,http://androidsmith.com/2011/07/using-gson-to-parse-json-on-android/或http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html或http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
答案 2 :(得分:2)
如果我这样做,我会将整个字符串解析为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"));
答案 3 :(得分:1)
这是一个使用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());
}
}
和对象i建模:
public class Flower {
private String category;
private double price;
private String instructions;
private String photo;
private String name;
private int productId;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
@Override
public String toString() {
return getProductId() + " : " + name + "\n" + price + "$" + "\n" + "\n";
}
答案 4 :(得分:0)
您发布的示例JSON数据似乎不遵循JSON的数据结构。您需要以与Mustafa发布的第三个link中所教导的方式完全相同的方式构建数据。这是一个非常棒的教程。我按照步骤操作,它确实有效!
答案 5 :(得分:0)
这是我用来解析嵌套JSON对象中所有记录的代码。
使用BSON时,它以object
的形式返回值。因此,为了使用与实际类型(int
,Document
,ArrayList
等)关联的方法,必须将返回的值强制转换为期望的类型。 / p>
// 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")));