2017-09-19 7 views
3

は、私は、JSONに取り組んでいると私は本当にこの構造を取得する必要があります: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

https://stackoverflow.com/questions/46190918/how-can-i-call-pro-img-in-this-json-formatこれはあなたの答えです。この質問を参照してください –

答えて

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側のロジックです

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(); 
     } 
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> 
-1

試してみる

writer.setIndent(" "); 
writer.beginObject(); 
writer.name("Identidade") 
writeArray(writer, util); 
writer.endObject(); 
writer.close(); 
0

trこれはObjectMapperジャクソンAPIを使用しています。

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)次にDTO参照上方含有れる、親Beanを書き込む:

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 
    } 
    ] 
} 
関連する問題