我正在研究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 **的提示吗?
答案 0 :(得分:2)
{
"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 side的逻辑
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();
}
答案 1 :(得分:0)
您必须创建JSON对象,将它们分配给JSON数组,然后将该数组分配给JSON对象。
JSONObject obj1 = new JSONObject();
obj1.put("numero", 1704);
obj1.put("numeroFinal", 1804);
obj1.put("id", 28);
JSONObject obj2 = new JSONObject();
obj2.put("numero", 1806);
obj2.put("numeroFinal", 1905);
obj2.put("id", 28);
JSONObject obj3 = new JSONObject();
obj3.put("numero", 1705);
obj3.put("numeroFinal", 1706);
obj3.put("id", 29);
JSONObject obj4 = new JSONObject();
obj4.put("numero", 1707);
obj4.put("numeroFinal", 1807);
obj4.put("id", 30);
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj1);
jsonArray.put(obj2);
jsonArray.put(obj3);
jsonArray.put(obj4);
JSONObject parent = new JSONObject();
parent.put("Identidade", jsonArray);
System.out.println(parent.toString());
输出:
{
"Identidade": [
{
"numeroFinal": 1804,
"numero": 1704,
"id": 28
},
{
"numeroFinal": 1905,
"numero": 1806,
"id": 28
},
{
"numeroFinal": 1706,
"numero": 1705,
"id": 29
},
{
"numeroFinal": 1807,
"numero": 1707,
"id": 30
}
]
}
如果你正在使用maven项目,你需要一个依赖项来使用JSONObject和JSONArray,在你的pom.xml中添加它:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20170516</version>
</dependency>
</dependencies>
答案 2 :(得分:0)
您可以尝试使用 Jackson API 的ObjectMapper
。
1)创建一个DTO类:
package com.multithreading.concurrency;
public class Identidade {
private int numero;
private int numeroFinal;
private int id;
public Identidade(int numero, int numeroFinal, int id) {
this.numero = numero;
this.numeroFinal = numeroFinal;
this.id = id;
}
// getters and setters
}
2)然后编写一个父bean,它将包含上面的DTO引用:
public class ParentIdentidade {
@JsonProperty("Identidade")
private List<Identidade> identidade;
// getters and setters
}
3)现在,编写测试类以产生所需的输出:
public class TestIdentidade {
public static void main(String[] args) {
List<Identidade> idenList = new ArrayList<Identidade>();
idenList.add(new Identidade(1704, 1804, 28));
idenList.add(new Identidade(1806, 1905, 28));
idenList.add(new Identidade(1705, 1706, 29));
idenList.add(new Identidade(1707, 1807, 30));
ParentIdentidade parentIden = new ParentIdentidade();
parentIden.setIdentidade(idenList);
ObjectMapper mapper = new ObjectMapper();
try {
String json = mapper.writeValueAsString(parentIden);
System.out.println(json);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
正在产生所需的输出:
{
"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
}
]
}
答案 3 :(得分:-1)
试试这个
writer.setIndent(" ");
writer.beginObject();
writer.name("Identidade")
writeArray(writer, util);
writer.endObject();
writer.close();