3

I want to create a JSON string like this:

[{"addon_id":2,"addon_rate":1550},{"addon_id":3, "addon_rate":300}]

Is this possible? How?

My code as it stands:

String formattedString = BookingDetailsCartAdapter.addones_id.toString().replace("[", "").replace("]", "").trim();
String  formattedString1 = BookingDetailsCartAdapter.addones_rate.toString().replace("[", "").replace("]", "").trim();

myjson="[{\"addon_id\":\""+formattedString+"\",\"addon_rate\":\""+formattedString1+"\"}]";
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Shameem Ahsan
  • 110
  • 1
  • 10

2 Answers2

4

You can use GSON

Add in Gradle file

compile 'com.google.code.gson:gson:2.8.1'


public class Addon{
    public int addon_id;
    public int addon_rate;
 }

Addon addon = new Addon ();
addon.addon_id=2;
addon.addon_id=1550;
Gson gson = new Gson();
String json = gson.toJson(addon); //

//--- For array

List<Addon > objList = new ArrayList<Addon >();
objList.add(new Addon (0, 1550));
objList.add(new Addon (1, 1552));
objList.add(new Addon (2, 1553));


String json = new Gson().toJson(objList);
System.out.println(json);

http://www.javacreed.com/simple-gson-example/

Debanik Dawn
  • 797
  • 5
  • 28
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • bad solution. He didnt ask to map model to json, he asked how to create a json like this above. Using third-party libs, create models, ... to generate json is not what he asked for – Emanuel Aug 21 '17 at 09:33
  • 1
    @EmanuelSeibold I disagree. This solution is fine. It brings to light, a very friendly way of generating Json that the OP is not aware of. – IAmGroot Aug 21 '17 at 09:55
  • 1
    and leads to more questions for a beginner. "What is Gson?" "How can i install GSON beside of the fact that the gradle import line is with a fixed version", " ..". The fact that it can be done without 3rd-party-libs misleads a beginner, even if i like gson for that case. – Emanuel Aug 21 '17 at 10:07
  • Introducing gson to op is good but is not the best way to solve the op problem, hence I agree with @EmanuelSeibold. Using a huge library instead of using a native one is a bit overkill. Let the op build his codeability gradually. – ישו אוהב אותך Aug 21 '17 at 11:03
  • 1
    There seems to be some good reasoning also in favor of GSON (see also https://stackoverflow.com/questions/42641301). We don't have enough information to know what's best in this case. I feel this is getting a bit opinionated. Both answers so far are legitimate solutions to the question. –  Aug 21 '17 at 11:13
  • It's all just mostly an opinion, But using JSONObject is more flexible in both parsing and creating a json if the API you're interacting with changes(it will change) in what it returns and takes in - especially if you're using the class to hold data in your program, which may then need you to refactor everything or change to an in-between class. Automapping them can really bite you, even if some(not all) will recommend using it because "it's the google way"(subject to change). If you just need to create a json from some data JSONObject is a very friendly/precise way to do it. – Lassi Kinnunen Nov 08 '18 at 04:16
4

Your JSON means that it's an array which contains two objects with the keys addon_id and addon_rate. Both accept a number/integer as value. First you need to create a JSONArray which holds several JSONObjects. Then you have to create the JSONObjects which set your keys and values. After this you need to add those objects to your array. Your jsonArray contains the string above as soon as you print/toString() it

JSONArray jsonArray = new JSONArray();  

JSONObject jsonObjOne = new JSONObject();
jsonObjOne.put("addon_id", 2);
jsonObjOne.put("addon_rate", 1550);

JSONObject jsonObjTwo = new JSONObject();
jsonObjTwo.put("addon_id", 3);
jsonObjTwo.put("addon_rate", 300);

jsonArray.put(jsonObjOne);
jsonArray.put(jsonObjTwo);
Emanuel
  • 8,027
  • 2
  • 37
  • 56