2

This is JSON file. I want make java can produce like this json. Just ignore the value, What i want is the structure of the json.

{
  "Car": [
    {
      "CarId": 123,
      "Status": "Ok"
    },
    {
      "CarId": 124,
      "Status": "ok"
    }
  ],
  "Motor": [
    {
      "MotorId": 3,
      "DriverId": 174
    },
    {
      "MotorId": 3,
      "DriverId": 174
    }
  ],
  "Bus": [
    {
      "BusId": 8,
      "Status": 3
    },
    {
      "BusId": 9,
      "Status": 2
    }
  ]
}

This is my java code.

JSONObject motorObject = new JSONObject();
JSONObject busObject = new JSONObject();
JSONObject carObject = new JSONObject();

JSONArray motorArray = new JSONArray();
JSONArray busArray = new JSONArray();
JSONArray carArray = new JSONArray();

motorArray.put(motorTracks.getJSONObject());
busArray.put(buss.getJSONObject());

try 
{
    motorObject.put("Motor",motorArray);
    busObject.put("Bus",busArray);

    carArray.put(MotorObject);
    carArray.put(stepObject);

    carObject.put("Car",dataArray);
} catch (JSONException e) 
{
    e.printStackTrace();
}

The output is :

{
  "Car": [
    {
      "Motor": [
        {
          "MotorId": 0,
          "DriverId": 0
        }
      ]
    },
    {
      "Bus": [
        {
          "BusId": 0,
          "Status": 0
        }
      ]
    }
  ]
}

For value, it's okay, just ignore the value, but what i want how can i get structure like the json file.

2 Answers 2

2

Use this code and Enjoy :)

private void createJsonStructure() {

    try
    {
        JSONObject rootObject = new JSONObject();

        JSONArray carArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("CarId", "123");
            jsonObject.put("Status", "Ok");
            carArr.put(jsonObject);
        }
        rootObject.put("Car", carArr);


        JSONArray motorArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("MotorId", "123");
            jsonObject.put("Status", "Ok");
            motorArr.put(jsonObject);
        }
        rootObject.put("Motor", motorArr);


        JSONArray busArr = new JSONArray();
        for (int i = 0; i < 2 ; i++)
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("BusId", "123");
            jsonObject.put("Status", "Ok");
            busArr.put(jsonObject);
        }
        rootObject.put("Bus", busArr);

        Log.e("JsonObject", rootObject.toString(4));

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks , i will try. Can i know why u put value 4 in rootObject.toString(4) ?
It will display the ooutput in logcat in Json like structure. You can try with value 4 and also without 4 and can see the difference.
Pleasure is all mine :)
yes adding the null value to Json doesn't accepted.
1
    JSONObject motorObject = new JSONObject();
    JSONObject busObject = new JSONObject();
    JSONObject carObject = new JSONObject();
    JSONObject wholeObject =new JSONObject();

    JSONArray motorArray = new JSONArray();
    JSONArray busArray = new JSONArray();
    JSONArray carArray = new JSONArray();

    motorArray.put(motorTracks.getJSONObject());
    busArray.put(buss.getJSONObject());
    carArray.put(car.getJSONObject());

    try
    {
        wholeObject.put("Motor",motorArray);
        wholeObject.put("Bus",busArray);
        wholeObject.put("Car",carArray);
        System.out.println(wholeObject);
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }

2 Comments

I try put jsonObject.put("BusId", null); ... But the output not show BusId
jsonObject wont consider the null values for a key.instead try put empty String ""

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.