简体   繁体   中英

How to pass array values in JSON?

ArrayList<StylistArray>stylistsArr=new ArrayList<StylistArray>();
stylistsArr.add(new StylistArray("BqYWKWzs4r8SyGQvyqH2","18:30"));
stylistsArr.add(new StylistArray("at5kjx5FqIxbnMys8w4q","18:30"));
stylistsArr.add(new StylistArray("nFI5hxfIePx240dmqR0R","18:00"));
stylistsArr.add(new StylistArray("spxSj8UZem5uD0wL46EP","18:20"));

.

https://us-central.jshshskajshdala.net?dasa="+"2018-09-04"+"&ewsss="+"17:50"+"&stylist="+String.valueOf(stylistsArr) 

I want to pass this arrayList along with the url. When I try to pass the array values the name of the model class is passing instead of values in the class

Example
I want to pass values like this:

[
  {
    "stylist": "BqYWKWzs4r8SyGQvyqH2",
    "endTime": "18:30"
  },
  {
    "stylist": "at5kjx5FqIxbnMys8w4q",
    "endTime": "18:30"
  },
  {
    "stylist": "nFI5hxfIePx240dmqR0R",
    "endTime": "18:00"
  },
  {
    "stylist": "spxSj8UZem5uD0wL46EP",
    "endTime": "18:00"
  }
]

Add the Gson dependency in gradle

implementation 'com.google.code.gson:gson:2.8.4'

and rebuild your project

String json= new Gson().toJson(stylistsArr);

Now your "json" variable will have a json array.

Like @Mathan and @Cris say, you can simply using for looping to create your JSON string. For the following simple pojo:

public class StylistArray {
  // use a final so the value can't be change once the object is created.
  private final String stylist;
  private final String endTime;

  public StylistArray(String stylist, String endTime) {
    this.stylist = stylist;
    this.endTime = endTime;
  }

  public String getStylist() {
    return stylist;
  }

  public String getEndTime() {
    return endTime;
  }
}

You can simply doing the following:

List<StylistArray> list = new ArrayList<>();
// assume you have add all items to the list.

JSONArray jsonArray = new JSONArray();
try {
  // You need to use simple for loop instead the following foreach
  // because foreach is slower than traditional loop.
  for (StylistArray stylistArray : list) {
    // create JSON object for each item
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("stylist", stylistArray.getStylist());
    jsonObject.put("endTime", stylistArray.getEndTime());

    // append it to your JSON array.
    jsonArray.put(jsonObject);
  }
} catch (JSONException e) {
  e.printStackTrace();
  // Error happens, try to handle it.
}

Then you can get the string from the JSONArray . I'll let you to find out ;)

Here is the code

List<Map<String, String>> values = new ArrayList<Map<String, String>>();
    Map<String, String> maps = new HashMap<>();
    maps.put("stylist", "BqYWKWzs4r8SyGQvyqH2");
    maps.put("endTime", "18:30");
    values.add(maps);
    maps.put("stylist", "at5kjx5FqIxbnMys8w4q");
    maps.put("endTime", "18:30");
    values.add(maps);
    maps.put("stylist", "nFI5hxfIePx240dmqR0R");
    maps.put("endTime", "18:00");
    values.add(maps);
    JSONArray mJSONArray = new JSONArray(values);

使用循环将是最好的选择,手动创建JSON,然后您可以为所需的字符串(造型师,endTime)分配希望准确赋予它们的值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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