如何将有序数据放入jsonObject?

时间:2016-06-22 18:16:26

标签: java android json

我是android studio和json的新手。 我在Android Studio中有o项目,我从服务器获取数据并将数据发送到服务器。

将数据发送到服务器时遇到问题。数据是对象类型,当我将数据放到jsonObject时,数据发送混合,我没有收到正确的数据(我发送的)。 这是我从服务器获取的对象:

{
   "success": true,
   "body": {
      "priority1": {
         "timeTable": {
            "fri": false,
            "time": {
               "op": true,
               "cl": false,
               "fh": null,
               "fm": null,
               "th": null,
               "tm": null
            },
            "mon": true,
            "sat": true,
            "sun": false,
            "thu": false,
            "tue": true,
            "wed": true
         },
         "number": "12345612332",
         "timeOut": "1"
      },
      "priority2": {
         "timeTable": {
            "fri": false,
            "time": {
               "op": true,
               "cl": false,
               "fh": null,
               "fm": null,
               "th": null,
               "tm": null
            },
            "mon": true,
            "sat": true,
            "sun": false,
            "thu": true,
            "tue": false,
            "wed": true
         },
         "number": "12345678901",
         "timeOut": "5"
      },
      "priority3": {
         "timeTable": {
            "time": {
               "op": true,
               "cl": false,
               "fh": null,
               "fm": null,
               "th": null,
               "tm": null
            },
            "mon": true,
            "tue": true,
            "wed": true,
            "thu": true,
            "fri": true,
            "sat": true,
            "sun": true
         },
         "number": null,
         "timeOut": null
      },
      "priority4": {
         "timeTable": {
            "time": {
               "op": true,
               "cl": false,
               "fh": null,
               "fm": null,
               "th": null,
               "tm": null
            },
            "mon": true,
            "tue": true,
            "wed": true,
            "thu": true,
            "fri": true,
            "sat": true,
            "sun": true
         },
         "number": null,
         "timeOut": null
      },
      "priority5": {
         "timeTable": {
            "time": {
               "op": true,
               "cl": false,
               "fh": null,
               "fm": null,
               "th": null,
               "tm": null
            },
            "mon": true,
            "tue": true,
            "wed": true,
            "thu": true,
            "fri": true,
            "sat": true,
            "sun": true
         },
         "number": null,
         "timeOut": null
      }
   },
   "error": null
}

这是我发送给服务器的对象:

{
   "priority5": {
      "number": null,
      "timeOut": null,
      "timeTable": {
         "fri": true,
         "mon": true,
         "sat": true,
         "sun": true,
         "thu": true,
         "time": {
            "cl": false,
            "fh": null,
            "fm": null,
            "op": true,
            "th": null,
            "tm": null
         },
         "tue": true,
         "wed": true
      }
   },
   "priority4": {
      "number": null,
      "timeOut": null,
      "timeTable": {
         "fri": true,
         "mon": true,
         "sat": true,
         "sun": true,
         "thu": true,
         "time": {
            "cl": false,
            "fh": null,
            "fm": null,
            "op": true,
            "th": null,
            "tm": null
         },
         "tue": true,
         "wed": true
      }
   },
   "priority1": {
      "number": "12345612332",
      "timeOut": "1",
      "timeTable": {
         "fri": false,
         "mon": true,
         "sat": true,
         "sun": false,
         "thu": false,
         "time": {
            "cl": false,
            "fh": null,
            "fm": null,
            "op": true,
            "th": null,
            "tm": null
         },
         "tue": true,
         "wed": true
      }
   },
   "priority3": {
      "number": null,
      "timeOut": null,
      "timeTable": {
         "fri": true,
         "mon": true,
         "sat": true,
         "sun": true,
         "thu": true,
         "time": {
            "cl": false,
            "fh": null,
            "fm": null,
            "op": true,
            "th": null,
            "tm": null
         },
         "tue": true,
         "wed": true
      }
   },
   "priority2": {
      "number": "12345678901",
      "timeOut": "5",
      "timeTable": {
         "fri": false,
         "mon": true,
         "sat": true,
         "sun": false,
         "thu": true,
         "time": {
            "cl": false,
            "fh": "02",
            "fm": "04",
            "op": false,
            "th": "08",
            "tm": "03"
         },
         "tue": false,
         "wed": true
      }
   }
}

这是我将数据放入jsonObject的set方法:

private void setExtensionForwardNumber() {

    if(priorityOne.isChecked()) {
        if (validateNumberField(priorityOneNumber)) {

            priorityResponse.priorityOne.number = priorityOneNumber.getText().toString();
            priorityResponse.priorityOne.timeOut = priorityOneTimeOut.getText().toString();
            priorityResponse.priorityOne.getTimeTable().mon = priorityOneMon.isChecked();
            priorityResponse.priorityOne.getTimeTable().tue = priorityOneTue.isChecked();
            priorityResponse.priorityOne.getTimeTable().wed = priorityOneWed.isChecked();
            priorityResponse.priorityOne.getTimeTable().thu = priorityOneThur.isChecked();
            priorityResponse.priorityOne.getTimeTable().fri = priorityOneFri.isChecked();
            priorityResponse.priorityOne.getTimeTable().sat = priorityOneSat.isChecked();
            priorityResponse.priorityOne.getTimeTable().sun = priorityOneSun.isChecked();
            priorityResponse.priorityOne.getTimeTable().getTime().open = priorityOne.isChecked();
            priorityResponse.priorityOne.getTimeTable().getTime().open = priorityOne24h.isChecked();
            if (!priorityOne24h.isChecked()) {
                priorityResponse.priorityOne.getTimeTable().getTime().fromHour = priorityOneFromHours.getSelectedItem().toString();
                priorityResponse.priorityOne.getTimeTable().getTime().fromMinute = priorityOneFromMinutes.getSelectedItem().toString();
                priorityResponse.priorityOne.getTimeTable().getTime().toHour = priorityOneToHours.getSelectedItem().toString();
                priorityResponse.priorityOne.getTimeTable().getTime().toMinute = priorityOneToMinute.getSelectedItem().toString();
            }
        }
    }else{
        priorityOne.setEnabled(false);
    }



    if(priorityTwo.isChecked()){
        if(validateNumberField(priorityTwoNumber)){
            priorityResponse.priorityTwo.number =priorityTwoNumber.getText().toString();
            priorityResponse.priorityTwo.timeOut = priorityTwoTimeOut.getText().toString();
            priorityResponse.priorityTwo.getTimeTable().mon = priorityTwoMon.isChecked();
            priorityResponse.priorityTwo.getTimeTable().tue = priorityTwoTue.isChecked();
            priorityResponse.priorityTwo.getTimeTable().wed = priorityTwoWed.isChecked();
            priorityResponse.priorityTwo.getTimeTable().thu = priorityTwoThur.isChecked();
            priorityResponse.priorityTwo.getTimeTable().fri = priorityTwoFri.isChecked();
            priorityResponse.priorityTwo.getTimeTable().sat = priorityTwoSat.isChecked();
            priorityResponse.priorityTwo.getTimeTable().sun = priorityTwoSun.isChecked();
            priorityResponse.priorityTwo.getTimeTable().getTime().open = priorityTwo.isChecked();
            priorityResponse.priorityTwo.getTimeTable().getTime().open = priorityTwo24h.isChecked();
            if(!priorityTwo24h.isChecked()){
                priorityResponse.priorityTwo.getTimeTable().getTime().fromHour = priorityTwoFromHours.getSelectedItem().toString();
                priorityResponse.priorityTwo.getTimeTable().getTime().fromMinute = priorityTwoFromMinutes.getSelectedItem().toString();
                priorityResponse.priorityTwo.getTimeTable().getTime().toHour = priorityTwoToHours.getSelectedItem().toString();
                priorityResponse.priorityTwo.getTimeTable().getTime().toMinute = priorityTwoToMinute.getSelectedItem().toString();

            }
        }
    }else{
        priorityTwo.setEnabled(false);
    }

    if(priorityThree.isChecked()){
        if(validateNumberField(priorityThreeNumber)){
            priorityResponse.priorityThree.number =priorityTwoNumber.getText().toString();
            priorityResponse.priorityThree.timeOut = priorityTwoTimeOut.getText().toString();
            priorityResponse.priorityThree.getTimeTable().mon = priorityThreeMon.isChecked();
            priorityResponse.priorityThree.getTimeTable().tue = priorityThreeTue.isChecked();
            priorityResponse.priorityThree.getTimeTable().wed = priorityThreeWed.isChecked();
            priorityResponse.priorityThree.getTimeTable().thu = priorityThreeThur.isChecked();
            priorityResponse.priorityThree.getTimeTable().fri = priorityThreeFri.isChecked();
            priorityResponse.priorityThree.getTimeTable().sat = priorityThreeSat.isChecked();
            priorityResponse.priorityThree.getTimeTable().sun = priorityThreeSun.isChecked();
            priorityResponse.priorityThree.getTimeTable().getTime().open = priorityThree.isChecked();
            priorityResponse.priorityThree.getTimeTable().getTime().open = priorityThree24h.isChecked();
            if(!priorityThree24h.isChecked()){
                priorityResponse.priorityThree.getTimeTable().getTime().fromHour = priorityThreeFromHours.getSelectedItem().toString();
                priorityResponse.priorityThree.getTimeTable().getTime().fromMinute = priorityThreeFromMinutes.getSelectedItem().toString();
                priorityResponse.priorityThree.getTimeTable().getTime().toHour = priorityThreeToHours.getSelectedItem().toString();
                priorityResponse.priorityThree.getTimeTable().getTime().toMinute = priorityThreeToMinute.getSelectedItem().toString();

            }
        }
    }else{
        priorityThree.setEnabled(false);
    }


    if(priorityFour.isChecked()){
        if(validateNumberField(priorityFourNumber)){
            priorityResponse.priorityFour.number =priorityFourNumber.getText().toString();
            priorityResponse.priorityFour.timeOut = priorityFourTimeOut.getText().toString();
            priorityResponse.priorityFour.getTimeTable().mon = priorityFourMon.isChecked();
            priorityResponse.priorityFour.getTimeTable().tue = priorityFourTue.isChecked();
            priorityResponse.priorityFour.getTimeTable().wed = priorityFourWed.isChecked();
            priorityResponse.priorityFour.getTimeTable().thu = priorityFourThur.isChecked();
            priorityResponse.priorityFour.getTimeTable().fri = priorityFourFri.isChecked();
            priorityResponse.priorityFour.getTimeTable().sat = priorityFourSat.isChecked();
            priorityResponse.priorityFour.getTimeTable().sun = priorityFourSun.isChecked();
            priorityResponse.priorityFour.getTimeTable().getTime().open = priorityFour.isChecked();
            priorityResponse.priorityFour.getTimeTable().getTime().open = priorityFour24h.isChecked();
            if(!priorityThree24h.isChecked()){
                priorityResponse.priorityFour.getTimeTable().getTime().fromHour = priorityFourFromHours.getSelectedItem().toString();
                priorityResponse.priorityFour.getTimeTable().getTime().fromMinute = priorityFourFromMinutes.getSelectedItem().toString();
                priorityResponse.priorityFour.getTimeTable().getTime().toHour = priorityFourToHours.getSelectedItem().toString();
                priorityResponse.priorityFour.getTimeTable().getTime().toMinute = priorityFourToMinute.getSelectedItem().toString();

            }
        }
    }else{
        priorityFour.setEnabled(false);
    }

    if(priorityFive.isChecked()){
        if(validateNumberField(priorityFiveNumber)){
            priorityResponse.priorityFive.number =priorityFiveNumber.getText().toString();
            priorityResponse.priorityFive.timeOut = priorityFiveTimeOut.getText().toString();
            priorityResponse.priorityFive.getTimeTable().mon = priorityFiveMon.isChecked();
            priorityResponse.priorityFive.getTimeTable().tue = priorityFiveTue.isChecked();
            priorityResponse.priorityFive.getTimeTable().wed = priorityFiveWed.isChecked();
            priorityResponse.priorityFive.getTimeTable().thu = priorityFiveThur.isChecked();
            priorityResponse.priorityFive.getTimeTable().fri = priorityFiveFri.isChecked();
            priorityResponse.priorityFive.getTimeTable().sat = priorityFiveSat.isChecked();
            priorityResponse.priorityFive.getTimeTable().sun = priorityFiveSun.isChecked();
            priorityResponse.priorityFive.getTimeTable().getTime().open = priorityFive.isChecked();
            priorityResponse.priorityFive.getTimeTable().getTime().open = priorityFive24h.isChecked();
            if(!priorityThree24h.isChecked()){
                priorityResponse.priorityFive.getTimeTable().getTime().fromHour = priorityFiveFromHours.getSelectedItem().toString();
                priorityResponse.priorityFive.getTimeTable().getTime().fromMinute = priorityFiveFromMinutes.getSelectedItem().toString();
                priorityResponse.priorityFive.getTimeTable().getTime().toHour = priorityFiveToHours.getSelectedItem().toString();
                priorityResponse.priorityFive.getTimeTable().getTime().toMinute = priorityFiveToMinute.getSelectedItem().toString();

            }
        }
    }else{
        priorityFive.setEnabled(false);
    }

    String s = priorityResponse.toJsonObject();
    setForwardExternalFragment(s);
}

请帮我解决我的问题。谢谢!

1 个答案:

答案 0 :(得分:1)

作为一般规则,JSON对象不是为维护顺序而设计的。您应该使用键访问值,在这种情况下,顺序是无关紧要的。

如果你真的必须维护秩序,你可以使用JSON数组。