在android中创建json格式

时间:2015-03-21 18:14:49

标签: android json

我怎样才能像这样在android json中创建。我可以创建json对象,但仍然面临一些问题。

{
"IncidentDetails": {
    "StreetId": "370","StartDate":"21-03-2015","EndDate":"24-03-2015",
    "ImageDetails": [
        {
            "PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
            "Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
        },
        {
            "PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
            "Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
        },
        {
            "PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
            "Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
        }
    ]}
}

我在我的Android代码中尝试的只是看看它提供完全不同的输出

JSONArray jsonArray = new JSONArray();
    JSONObject IncidentDetails = new JSONObject();

    try 
    {
        IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
        IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
        IncidentDetails.put("StreetId", StreetId);
        jsonArray.put(IncidentDetails);

        for(int i=0; i<length; i++)
        {

            JSONObject ImageDetails = new JSONObject();
            ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
            ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
            jsonArray.put(ImageDetails);
        }

        JSONObject IncidentDetailsObj = new JSONObject();
        IncidentDetailsObj.put("IncidentDetails", jsonArray);

        String jsonStr = IncidentDetailsObj.toString();

我当前的输出

{"IncidentDetails":[{"StreetId":"370","StartDate":"18-03-2015","EndDate":""},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"}]}

3 个答案:

答案 0 :(得分:1)

使用此

JSONArray jsonArray = new JSONArray();
JSONObject IncidentDetails = new JSONObject();

    try 
    {
        IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
        IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
        IncidentDetails.put("StreetId", StreetId);


        for(int i=0; i<length; i++)
        {

            JSONObject ImageDetails = new JSONObject();
            ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
            ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
            jsonArray.put(ImageDetails);
        }
    IncidentDetails.put("ImageDetails", jsonArray);

        JSONObject IncidentDetailsObj = new JSONObject();
        IncidentDetailsObj.put("IncidentDetails", IncidentDetails);

        String jsonStr = IncidentDetailsObj.toString();

答案 1 :(得分:1)

见评论回答,

   //* IncidentalDetial is an object not an array
   JSONObject IncidentDetails = new JSONObject();
   //* add the first three elements
   IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
   IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
   IncidentDetails.put("StreetId", StreetId);
   //* contruct the imagedetails array
   JSONArray jsonArray = new JSONArray();
   for(int i=0; i<length; i++)
   {
     JSONObject ImageDetails = new JSONObject();
     ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
     ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
     jsonArray.put(ImageDetails);
   }
   //* add the imagedetails array to the incidentDetials object
   IncidentDetails.put(jsonArray);

  JSONObject IncidentDetailsObj = new JSONObject();
  IncidentDetailsObj .put(IncidentDetails);
   String jsonStr = IncidentDetailsObj.toString();

答案 2 :(得分:1)

请注意,这是非常常见的情况,因此必须有一些库为您处理它:)

我推荐https://code.google.com/p/google-gson - GSON。您所要做的就是准备描述JSON的POJO类,在您的情况下:

public class IncidentDetails {
    IncidentDetails incidentDetails;
}

public class IncidentDetails {
    String streetId;
    String startDate;
    String endDate;
    ImageDetails imageDetails;
}

然后对ImageDetails执行相同的操作。您所要做的就是使用适当的命名策略创建GSON(它将automaticcaly类字段名称转换为JSON)。

gson.toJson(incidentDetails)创建您想要的String。 gson.fromString(String json)将从json字符串创建IncidentDetails对象。