【问题标题】:How to structure Multiple object and array of JSON in java android如何在java android中构造多个JSON对象和数组
【发布时间】:2017-09-05 02:20:12
【问题描述】:

这是 JSON 文件。我想让java可以产生这样的json。忽略这个值,我想要的是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
    }
  ]
}

这是我的 java 代码。

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();
}

输出是:

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

对于价值,没关系,忽略价值,但我想要的我怎样才能得到像json文件这样的结构。

【问题讨论】:

    标签: java android arrays json object


    【解决方案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();
        }
    

    【讨论】:

    • 我试着把 jsonObject.put("BusId", null); ...但输出不显示 BusId
    • jsonObject 不会考虑键的空值。而是尝试放置空字符串“”
    【解决方案2】:

    使用此代码并享受 :)

    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();
        }
    }
    

    【讨论】:

    • 谢谢,我会努力的。我能知道为什么你把值 4 放在 rootObject.toString(4) 中吗?
    • 它将以类似Json的结构显示logcat中的ooutput。您可以尝试使用值 4,也可以尝试不使用 4,并且可以看到差异。
    • 快乐是我的 :)
    • 是的,不接受将空值添加到 Json。
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 2021-12-07
    • 1970-01-01
    • 2015-01-05
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多