gpt4 book ai didi

java - 如何在Json中定义一个数据结构,结构的顶部是对象的名称?

转载 作者:太空狗 更新时间:2023-10-29 13:51:37 26 4
gpt4 key购买 nike

我正在处理 json,我真的需要得到这个结构:

{

"Identidade":

[

{ "numero": 1704, "numeroFinal": 1804, "id": 28 },
{ "numero": 1806, "numeroFinal": 1905, "id": 28 },
{ "numero": 1705, "numeroFinal": 1706, "id": 29 },
{ "numero": 1707, "numeroFinal": 1807, "id": 30 }

]

}

但是到现在我只能弄到这一个,还得能在最上面写上**Identidade**

   [ 

{ "numero": 1704, "numeroFinal": 1804, "id": 28 },
{ "numero": 1806, "numeroFinal": 1905, "id": 28 },
{ "numero": 1705, "numeroFinal": 1706, "id": 29 },
{ "numero": 1707, "numeroFinal": 1807, "id": 30 }

]

下面的代码是我当前的实现。

public void writeJsonStream(String file, List<Identidade> iden) throws IOException {

JsonWriter writer = new JsonWriter(new OutputStreamWriter(m_Context.openFileOutput(file, Context.MODE_PRIVATE)));
writer.setIndent(" ");
writeArray(writer, util);
writer.close();
}


public void writeArray(JsonWriter writer, List<Identidade> iden) throws IOException {
writer.beginArray();
for (Identidade i : iden) {
writeIdentidade(writer, i);
}
writer.endArray();
}

public void writeIdentidade(JsonWriter writer, Identidade iden) throws IOException

writer.beginObject();
writer.name("numero").value(iden.getM_numero());
writer.name("numeroFinal").value(iden.numeroFinal());
writer.name("id").value(iden.getID());
writer.endObject();
}

有人可以提示我如何添加 ** Identidade** 吗?

最佳答案

 {
"status": "100",
"cart_qty": 0,
"user": [
{
"pro_id": "63",
"pro_title": "Nikon S9400 Advanced Point & Shoot Camera (Red)",
"pro_price": "16000.00",
"pro_disprice": "12000.00",
"pro_discount_percentage": 25,
"pro_Img": [
"http://backslashinfotech.in/laravel_Ecommerce/assets/product/nikon-coolpix-s9400-advance-point-and-shoot-original-imadgx8twu6buaag.jpeg",
"http://backslashinfotech.in/laravel_Ecommerce/assets/product/nikon-coolpix-s9400-advance-point-and-shoot-original-imadgx8ty6rvx2dn.jpeg"
],
"created_date": "07/13/2017",
"pro_image_count": "1",
"pro_qty": "23",
"hit_count": "0",
"sold_status": "1",
"whishlist": 0
},
]
}

这是json,下面是android端的逻辑

try {
JSONObject jsonObject= new JSONObject("respones");
String status=jsonObject.getString("status");
String scart_qty=jsonObject.getString("cart_qty");

JSONArray user= jsonObject.getJSONArray("user");
for (int i=0;i<user.length();i++){
JSONObject json_data = user.getJSONObject(i);
Log.e("pro_title",json_data.getString("pro_title"));
Log.e("pro_price",json_data.getString("pro_price"));
Log.e("pro_disprice",json_data.getString("pro_disprice"));
Log.e("pro_discount_percentage",json_data.getString("pro_discount_percentage"));


JSONArray itemArray=json_data.getJSONArray("pro_Img");
for (int j = 0; j < itemArray.length(); j++) {
String value=itemArray.getString(j);
Log.e("PHOTOS_URL", j+"="+value);
}
}
} catch (JSONException e) {
e.printStackTrace();
}

关于java - 如何在Json中定义一个数据结构,结构的顶部是对象的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com