0

I want to create JSONArray like this

enter image description here

when i add json object in array it will format like this

enter image description here

My Code:

JSONObject object = new JSONObject();
object.put("calories_burn", "345");
object.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
JSONObject object1 = new JSONObject();
object1.put("calories", object);
array.put(object1);
4
  • 1
    Create a list of json object with there identifier and transform the list into json array Commented Jan 24, 2017 at 19:08
  • i'm confused what you are trying to do. Are you trying to create an array and in that array an object with the name of android and withing that object a new array with steps, calories etc. Or is your array named android and you want it to contain objects steps, calories, bpm? Commented Jan 24, 2017 at 19:09
  • i want to create can you make this format as shown in first image. Commented Jan 24, 2017 at 19:13
  • @Rod_Algonquin how can i add identifier to a json object? i dint find any method for that? Commented Jan 24, 2017 at 19:19

2 Answers 2

2

You are falling in between two patterns, you can either use it as a map, or an array.

Map approach:

{
  "steps": { "steps":123, "time": 123 },
  "calories": { and so},
  "bpm": { on }
}

Code (untested, think and tweak)

// Build your objects
JSONObject steps = new JSONObject();
steps.put("steps", 123);
steps.put("time" 123);
JSONObject calories = new JSONObject();
//And so on
JSONObject bpm = new JSONObject();

//Make a map
JSONObject map = new JSONObject();
//Add to the map:
map.put("steps", steps);
map.put("calories", calories);
map.put("bpm", bpm);
Sign up to request clarification or add additional context in comments.

3 Comments

how can i do this in java
its still not created array {"steps":{"steps":"27200","time":1485287743921},"calories":{"calories_burn":4953,"time":1485287743921},"bpm":{"bpm":83,"time":1485287743921}}
I already told you that you can either chose to do an array or a map, you already succeeded in making an array, and I showed you how to make a map, the code you show me is exactly as shown in my example. What you ask for in your example, is not valid json.
0

No, you cannot add keys inside a JSONArray. You can do that inside a JSONObject though.

Two approaches:

  1. With JSONArray (Acts Like A List):

    JSONArray jsonArray = new JSONArray();
    
    JSONObject caloriesJSON = new JSONObject();
    caloriesJSON.put("calories_burn", 345);
    caloriesJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
    
    JSONObject stepsJSON = new JSONObject();
    stepsJSON.put("steps", "12121");
    stepsJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
    
    jsonArray.put(stepsJSON);
    jsonArray.put(caloriesJSON);
    
  2. With JSONObject(Acts Like A Map/Dictionary):

    JSONObject jsonObject = new JSONObject();
    
    JSONObject caloriesJSON = new JSONObject();
    caloriesJSON.put("calories_burn", 345);
    caloriesJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
    
    JSONObject stepsJSON = new JSONObject();
    stepsJSON.put("steps", "12121");
    stepsJSON.put("time", dp.getTimestamp(TimeUnit.MILLISECONDS));
    
    jsonObject.put("steps",  stepsJSON);
    jsonObject.put("calories", caloriesJSON);
    

Be careful what you put for values. "12313" is a String whereas 345 is an integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.