如何在JsonArray中存储多行?

时间:2018-11-01 11:20:41

标签: android json

问题在于,第一行中的数据正常保存,但第二行中的数据正常保存,或者继续进行数据冗余操作,例如:

 public void   Save(){
    String register;
    JSONObject jsonObj;
    JSONObject json;
    JSONArray jsonTempArray = new JSONArray();
    JSONObject jsonObject=new JSONObject();
    JSONArray jsonArray=new JSONArray();

    try{
        JSONObject jsonObjs=new JSONObject();

        jsonObj=new JSONObject();
        json=new JSONObject();       

        Integer.valueOf(mGeneralID);
        str_UpdatedGeneralID= "6832";

        String str_FMeasurementSamplesize = "";
        if (txtFMeasurementSamplesize.getText().toString().equals("")){
            str_FMeasurementSamplesize = "0";
        }else {
            str_FMeasurementSamplesize = txtFMeasurementSamplesize.getText().toString();
        }

        String Samplesize = txt_Sample_Size.getText().toString();
        String Qtypersize = txt_Qty_per_Size.getText().toString();
        String TotalnoofmeasPointsizeA = txtTotalnoOfmeasPointsizeA.getText().toString();
        String TotalnoofmeasPointsizeAA = txtTotalnoOfmeasPointsizeAA.getText().toString();
        String TotalnoofmeasPointsizeB = txtTotalnoOfmeasPointsizeB.getText().toString();
        String TotalnoofmeasPointsizeBB = txtTotalnoOfmeasPointsizeBB.getText().toString();
        String NomeasurementptoutoftolA = txtNomeasurementptoutoftolA.getText().toString();
        String NomeasurementptoutoftolB = txtNomeasurementptoutoftolB.getText().toString();

        String PASS = txt_pass.getText().toString();
        String Fail = txt_fail.getText().toString();
        String Pending = txt_pending.getText().toString();
        String Remarks = txt_Remarks_F.getText().toString();

        DateFormat df = new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
        String CreationDate = df.format(Calendar.getInstance().getTime());

        int a =0;
        for (int i=0; i < MeasurementArrayList.size(); i++){

            String MeasurementSheetDetailID = MeasurementArrayList.get(i).getmeasurementSheetDetailID();
            String MeasurementPointID = MeasurementArrayList.get(i).getmeasurementPointID();
            String MeasurementPoints = MeasurementArrayList.get(i).getmeasurementPoints();
            String SpecSizeA = MeasurementArrayList.get(i).getSpecSizeA();
            String SpecSizeAOne = MeasurementArrayList.get(i).getSpecSizeAOne();
            String SpecSizeATwo = MeasurementArrayList.get(i).getSpecSizeATwo();
            String SpecSizeAThree = MeasurementArrayList.get(i).getSpecSizeAThree();
            String SpecSizeAFour = MeasurementArrayList.get(i).getSpecSizeAFour();
            String SpecSizeAFive = MeasurementArrayList.get(i).getSpecSizeAFive();
            String SpecSizeB = MeasurementArrayList.get(i).getSpecSizeB();
            String SpecSizeBOne = MeasurementArrayList.get(i).getSpecSizeBOne();
            String SpecSizeBTwo = MeasurementArrayList.get(i).getSpecSizeBTwo();
            String SpecSizeBThree = MeasurementArrayList.get(i).getSpecSizeBThree();
            String SpecSizeBFour = MeasurementArrayList.get(i).getSpecSizeBFour();
            String SpecSizeBFive = MeasurementArrayList.get(i).getSpecSizeBFive();
            String Tolerance = MeasurementArrayList.get(i).gettolerance();

            jsonObj.put("MeasurementSheetID", str_FMeasurementSamplesize);
            jsonObj.put("GeneralID", str_UpdatedGeneralID);
            jsonObj.put("Samplesize", Samplesize);
            jsonObj.put("Measurement", str_sp_Measurement);
            jsonObj.put("Qtypersize", Qtypersize);
            jsonObj.put("Color", str_sp_Color);
            jsonObj.put("SpecSizeA", str_sp_SpecSizeA);
            jsonObj.put("SpecSizeB", str_sp_SpecSizeB);
            jsonObj.put("TotalnoofmeasPointsizeA",TotalnoofmeasPointsizeA);
            jsonObj.put("TotalnoofmeasPointsizeAA", TotalnoofmeasPointsizeAA);
            jsonObj.put("TotalnoofmeasPointsizeB", TotalnoofmeasPointsizeB);
            jsonObj.put("TotalnoofmeasPointsizeBB", TotalnoofmeasPointsizeBB);
            jsonObj.put("NomeasurementptoutoftolA", NomeasurementptoutoftolA);
            jsonObj.put("NomeasurementptoutoftolB", NomeasurementptoutoftolB);
            jsonObj.put("PASS", PASS);
            jsonObj.put("Fail", Fail);
            jsonObj.put("Pending", Pending);
            jsonObj.put("Remarks", Remarks);
            jsonObj.put("CreationDate", CreationDate);

            jsonObj.put("MeasurementSheetDetailID", MeasurementSheetDetailID);
            jsonObj.put("MeasurementPointID", MeasurementPointID);
            jsonObj.put("MeasurementPoints", MeasurementPoints);
            jsonObj.put("SpecSizeA", SpecSizeA);
            jsonObj.put("SpecSizeAOne", SpecSizeAOne);
            jsonObj.put("SpecSizeATwo", SpecSizeATwo);
            jsonObj.put("SpecSizeAThree", SpecSizeAThree);
            jsonObj.put("SpecSizeAFour", SpecSizeAFour);
            jsonObj.put("SpecSizeAFive", SpecSizeAFive);
            jsonObj.put("SpecSizeB", SpecSizeB);
            jsonObj.put("SpecSizeBOne", SpecSizeBOne);
            jsonObj.put("SpecSizeBTwo", SpecSizeBTwo);
            jsonObj.put("SpecSizeBThree", SpecSizeBThree);
            jsonObj.put("SpecSizeBFour", SpecSizeBFour);
            jsonObj.put("SpecSizeBFive", SpecSizeBFive);
            jsonObj.put("Tolerance", Tolerance);

            jsonTempArray.put(jsonObj);

        }

        jsonObject.put("jsondata",jsonTempArray);
        register=jsonObject.toString();

    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }
    new save(register).execute();
}

结果:两行都是问题所在

第1行

  

[{“ MeasurementSheetID”:“ 0”,“ GeneralID”:“ 6832”,“ Samplesize”:“ 10”,“ Measurement”:“ INCH”,“ Qtypersize”:“ 5”,“ Color”:“黑色“,” SpecSizeA“:” 6.5“,” SpecSizeB“:” 6.5“,” TotalnoofmeasPointsizeA“:” 30“,” TotalnoofmeasPointsizeAA“:” 125“,” TotalnoofmeasPointsizeB“:” 30“,” TotalnoofmeasPointsizeBB“:” 125“ ,“ NomeasurementptoutoftolA”:“ 0”,“ NomeasurementptoutoftolB”:“ 0”,“ PASS”:“ d”,“ Fail”:“ h”,“ Pending”:“ k”,“备注”:“ g”,“ CreationDate“:” 2018-11-305   05:04:28“,” MeasurementSheetDetailID“:” 0“,” MeasurementPointID“:” 2“,” MeasurementPoints“:”返回   口袋位置位置外   EDGE“,” SpecSizeAOne“:”“,” SpecSizeATwo“:”“,” SpecSizeAThree“:”“,” SpecSizeAFour“:”“,” SpecSizeAFive“:”“,” SpecSizeBOne“:”“,” SpecSizeBTwo“:” “,” SpecSizeBThree“:”“,” SpecSizeBFour“:”“,” SpecSizeBFive“:”“,” Tolerance“:” +/- 0.5“},

第2行

  

{“ MeasurementSheetID”:“ 0”,“ GeneralID”:“ 6832”,“ Samplesize”:“ 10”,“ Measurement”:“ INCH”,“ Qtypersize”:“ 5”,“ Color”:“黑色“,” SpecSizeA“:” 6.5“,” SpecSizeB“:” 6.5“,” TotalnoofmeasPointsizeA“:” 30“,” TotalnoofmeasPointsizeAA“:” 125“,” TotalnoofmeasPointsizeB“:” 30“,” TotalnoofmeasPointsizeBB“:” 125“, “ NomeasurementptoutoftolA”:“ 0”,“ NomeasurementptoutoftolB”:“ 0”,“ PASS”:“ d”,“ Fail”:“ h”,“ Pending”:“ k”,“备注”:“ g”,“ CreationDate “:” 2018-11-305   05:04:28“,” MeasurementSheetDetailID“:” 0“,” MeasurementPointID“:” 2“,” MeasurementPoints“:”返回   口袋位置位置外   EDGE“,” SpecSizeAOne“:”“,” SpecSizeATwo“:”“,” SpecSizeAThree“:”“,” SpecSizeAFour“:”“,” SpecSizeAFive“:”“,” SpecSizeBOne“:”“,” SpecSizeBTwo“:” “,” SpecSizeBThree“:”“,” SpecSizeBFour“:”“,” SpecSizeBFive“:”“,” Tolerance“:” +/- 0.5“}]

2 个答案:

答案 0 :(得分:0)

您需要将此jsonObj=new JSONObject();放入循环中。

因此,每次执行循环时,都会创建新的JSONObject并将其放入JSONArray

答案 1 :(得分:0)

JsonArray由jsonObjects组成。 您需要清空jsonobject,将其填充并为jsonArray中的每个对象放入jsonarray。

jsonObj = new JSONObject();

Some documentation