简体   繁体   English

如何在Json中定义数据结构,对象的名称位于结构的顶部?

[英]How to define a data structure in Json, with the name of the object at the top of the structure?

I'm working on json and I really need to get this structure: 我正在研究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 }

       ]

}

But until now I can only get this one, and I still have to be able to write the ** Identidade** at the top 但是直到现在我才能得到这个,我仍然必须能够在顶部写下** 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 }

   ]

The code that follows is my current implementation. 下面的代码是我当前的实现。

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

Can someone give me a hint on how to add ** Identidade**? 有人能给我一个如何添加** 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
    },
  ]
}

this is json and below is logic of android side 这是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();
        }

You have to create the JSON objects, assign them to a JSON array, then assign the array to a JSON object. 您必须创建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());

output: 输出:

{
  "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
    }
  ]
}

You'll need a dependency to use JSONObject and JSONArray, if you're using a maven project add this in your pom.xml: 你需要依赖来使用JSONObject和JSONArray,如果你正在使用maven项目在你的pom.xml中添加它:

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>
    </dependencies>

You can try this which uses ObjectMapper of Jackson API . 您可以尝试使用Jackson API的 ObjectMapper

1) Create a DTO class : 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) Then write a parent bean, which will contain the above DTO reference : 2)然后编写一个父bean,它将包含上面的DTO引用:

public class ParentIdentidade { 

    @JsonProperty("Identidade")
    private List<Identidade> identidade;
    // getters and setters
}

3) Now, write the test class to produce the desired output : 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();
    }
  }
}

It is producing the desired output : 它产生了所需的输出:

{
"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
    }
  ]
}

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM