简体   繁体   English

如何在JAVA中打印对象数组

[英]How to print object array in JAVA

I want to print from object like this : 我想从这样的对象打印:

{"data":[["Tiger Nixon","System  
  Architect","Edinburgh","5421","2011\/04\/25","$320,800"],["Garrett     
  Winters","Accountant","Tokyo","8422","2011\/07\/25","$170,750"]]}

i tried in java like this : 我在Java中这样尝试过:

ArrayList<String> list = new ArrayList<String>();
list.add("Tiger Nixon");
list.add("System Architect");
list.add("Edinburgh");
list.add("2011\/04\/25");
list.add("$320,800");
JSONArray stateArray = JSONFactoryUtil.createJSONArray();
JSONObject stateObject;

stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("data", list);  // i got an error in here

stateArray.put(stateObject);
System.out.println(stateArray);

But, i don't know how to implement object like that. 但是,我不知道如何实现这样的对象。 There's anyone can help me ? 有谁可以帮助我?

You can achieve your objective like this: 您可以这样实现目标:

JSONArray stateArray = JSONFactoryUtil.createJSONArray();
stateArray.put("Tiger Nixon");
stateArray.put("System Architect");
stateArray.put("Edinburgh");
stateArray.put("2011\/04\/25");
stateArray.put("$320,800");

JSONObject stateObject;
stateObject = JSONFactoryUtil.createJSONObject();
stateObject.put("data", stateArray);
System.out.println(stateObject.toString());
ArrayList <String> list = new ArrayList <String>();
list.add("Tiger Nixon");
list.add("System Architect");
JSONArray array = new JSONArray();
for (int i = 0; i < list.size(); i++) {
        array.put(list.get(i));
}
JSONObject object = new JSONObject();
try {
    obj.put("data", array);
} catch (JSONException e) {
e.printStackTrace();
}

Note :- Here JSONObject class whatever i used belongs to org.json.simple Jar Only. 注意 :-这里的JSONObject类无论我使用什么都属于org.json.simple Jar Only。 I used this class org.json.simple.JSONObject 我使用了此类org.json.simple.JSONObject

What you have tried is. 您尝试过的是。 You are trying to create a JSONObject of Type ArrayList<String> which will not give you the expected Result, because your json array format is such like that 您正在尝试创建ArrayList<String>类型的JSONObject,它将不会为您提供预期的结果,因为您的json数组格式是这样的

[["","",""],["","",""],["","",""],["","",""]] // arraylist inside an arraylist.

Your format like that a ArrayList inside an ArrayList but what you try is using only one ArrayList . 您的格式类似于ArrayListArrayList但是您尝试的是仅使用一个ArrayList

Here is your Code. 这是您的代码。

 ArrayList<String> list = new ArrayList<String>(); //only one arrayList.
    list.add("Tiger Nixon"); // adding values to the list.
    list.add("System Architect");
    list.add("Edinburgh");
    list.add("2011\/04\/25");
    list.add("$320,800");

Your Code will give output like this.. 您的代码将给出这样的输出。

[{"data":"["","",""]"}, {"data":"["","",""]"}, {"data":"["","",""]"}]

Try this code instead of yours (below is just for sample use please try to manipulate this logic as per your need). 尝试使用此代码而不是您的代码(以下仅供示例使用,请尝试根据需要操作此逻辑)。 this will give you the expected Result. 这将给您预期的结果。

ArrayList<ArrayList<String>> final_arrObject = new ArrayList<ArrayList<String>();
 ArrayList<String> list;
 for(int i=0; i<5; i++){ //demo to add 5 ArrayList<String> to an ArrayList<ArrayList<String>>.
    list = new ArrayList<String>(); //only one arrayList.

     //at each iteration you may change you ArrayList Values.
    list.add("Tiger Nixon");    // adding values to the list.
    list.add("System Architect");
    list.add("Edinburgh");
    list.add("2011\/04\/25");
    list.add("$320,800");
    final_arrObject.add(list);
}

JSONObject obj_finalJSON= new JSONObject();
obj_finalJSON.put("data", final_arrObject);
System.out.println(obj_finalJSON.toString());  //it will print your json as a String.

There is a model,and some properties in it. 有一个模型,其中有一些属性。 You can new some model info ,and add it to a List. 您可以添加一些模型信息,并将其添加到列表中。 Then put this list to JSON,and print it json.toString(). 然后将此列表放入JSON,并将其打印为json.toString()。 If you want specific code, send message to me! 如果您需要特定的代码,请给我发送消息!

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

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