JSON文件在少数条目后返回空值

时间:2015-09-25 05:29:08

标签: android json

我的JSON文件只返回存储在其中的前四个值,然后将其返回为null。

这是我的JSON文件编写代码

JSONArray data = new JSONArray();
        JSONObject object = new JSONObject();
        try
        {


            object.put("Event Name: ", Event);
            object.put("College Name: ", College);
            object.put("Category: ", Category);
            object.put("Sub-Category: ", Sub);
            object.put("Date From: ", Dfrom);
            object.put("Date to :", Dto);
            object.put("City: ", City);
            object.put("State: ", State);
            object.put("Venue: ", Venue);
            object.put("Website: ", URL);
            object.put("Registration Form Link: ", Form);
            object.put("Contact Number: ", Number);
            object.put("E-mail Id: ", Email);

            data.put(object);

            String text = data.toString();
            FileOutputStream fos = openFileOutput("event.json", MODE_PRIVATE);
            OutputStreamWriter writer = new OutputStreamWriter(fos);
            writer.write(text);
            writer.flush();
            writer.close();

            Toast.makeText(getApplicationContext(), "Event Successfully Submitted", Toast.LENGTH_SHORT)
                    .show();

         }

其中Dfrom和Dto是DatePicker选择的日期。

这是我的JSON文件读取编码

public void readForm()
    {

    String path = getFilesDir().getAbsolutePath() +"\n"+ "/event.json";
    File f = new File(path);
    f.setReadable(true, false);

    try
    {
        FileInputStream fis = openFileInput("event.json");
        BufferedInputStream bis = new BufferedInputStream(fis);
        StringBuffer b = new StringBuffer();
        while (bis.available()!=0)
        {
            char c = (char) bis.read();
            b.append(c);
        }
        bis.close();
        fis.close();

        JSONArray data = new JSONArray(b.toString());

        event1 = data.getJSONObject(0).getString("Event Name: ");
        college1 = data.getJSONObject(0).getString("College Name: ");
        category1 = data.getJSONObject(0).getString("Category: ");
        sub1 = data.getJSONObject(0).getString("Sub-Category: ");
        dfrom1 = data.getJSONObject(0).getString("Date From:");
        dto1 = data.getJSONObject(0).getString("Date To: ");
        city1 = data.getJSONObject(0).getString("City: ");
        state1 = data.getJSONObject(0).getString("State: ");
        venue1 = data.getJSONObject(0).getString("Venue: ");
        url1 = data.getJSONObject(0).getString("Website: ");
        form1 = data.getJSONObject(0).getString("Registration Form Link: ");
        number1 = data.getJSONObject(0).getString("Contact Number: ");
        email1 = data.getJSONObject(0).getString("E-mail Id: ");



    }

之后,我将这些值传递给TextViews,只有前四个返回值,其余为null。

DatePicker编码

DatePickerDialog.OnDateSetListener from_dateListener  = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            abcd = (new StringBuilder().append(day)
                    .append("-").append(month + 1).append("-").append(year)
                    .append(" ")).toString();
            from.setText(abcd);
            Dfrom = abcd;
            // set selected date into datepicker also
        }

    };

即使我尝试显示从Dfrom到电子邮件的值,所有都返回null 请帮忙

2 个答案:

答案 0 :(得分:0)

我认为问题是关键值未命中匹配

例如

您正在设置

object.put("Date From: ", Dfrom);
object.put("Date to :", Dto);

试图获得

dfrom1 = data.getJSONObject(0).getString("Date From:");
  dto1 = data.getJSONObject(0).getString("Date To: ");

在"日期之间做差异:"和"日期自:"是"之后的空间:" 所以请交叉检查所有。

答案 1 :(得分:0)

您的密钥可能存在问题,因为密钥在json中不匹配:

尝试以下代码:

public static final String KEY_EVENT = "event";
    public static final String KEY_COLLEGE = "college";
    public static final String KEY_CATEGORY = "category";
    public static final String KEY_SUB_CATEGORY = "sub-category";
    public static final String KEY_DATE_TO = "dateto";
    public static final String KEY_DATE_FROM = "datefrom";
    public static final String KEY_CITY = "city";
    public static final String KEY_STATE = "state";
    public static final String KEY_VENUE = "venue";
    public static final String KEY_WEBSITE = "website";
    public static final String KEY_LINK_REGISTRATION = "registration";
    public static final String KEY_CONTACT = "contact";
    public static final String KEY_EMAIL = "email";
 public void writeFile() {
        JSONArray data = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put(KEY_EVENT, "name");
            object.put(KEY_COLLEGE, "college");
            object.put(KEY_CATEGORY, "category");
            object.put(KEY_SUB_CATEGORY, "sub");
            object.put(KEY_DATE_FROM, "dfrom");
            object.put(KEY_DATE_TO, "dto");
            object.put(KEY_CITY, "city");
            object.put(KEY_STATE, "state");
            object.put(KEY_VENUE, "venue");
            object.put(KEY_WEBSITE, "url");
            object.put(KEY_LINK_REGISTRATION, "form");
            object.put(KEY_CONTACT, "number");
            object.put(KEY_EMAIL, "email");

            data.put(object);

            String text = data.toString();
            FileOutputStream fos = openFileOutput("event.json", MODE_PRIVATE);
            OutputStreamWriter writer = new OutputStreamWriter(fos);
            writer.write(text);
            writer.flush();
            writer.close();

            Toast.makeText(getApplicationContext(), "Event Successfully Submitted", Toast.LENGTH_SHORT)
                    .show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void readFile() {
        String path = getFilesDir().getAbsolutePath() + "\n" + "/event.json";
        File f = new File(path);
        f.setReadable(true, false);

        try {
            FileInputStream fis = openFileInput("event.json");
            BufferedInputStream bis = new BufferedInputStream(fis);
            StringBuffer b = new StringBuffer();
            while (bis.available() != 0) {
                char c = (char) bis.read();
                b.append(c);
            }
            bis.close();
            fis.close();

            JSONArray data = new JSONArray(b.toString());

            String event1 = data.getJSONObject(0).getString(KEY_EVENT);
            String college1 = data.getJSONObject(0).getString(KEY_COLLEGE);
            String category1 = data.getJSONObject(0).getString(KEY_CATEGORY);
            String sub1 = data.getJSONObject(0).getString(KEY_SUB_CATEGORY);
            String dfrom1 = data.getJSONObject(0).getString(KEY_DATE_FROM);
            String dto1 = data.getJSONObject(0).getString(KEY_DATE_TO);
            String city1 = data.getJSONObject(0).getString(KEY_CITY);
            String state1 = data.getJSONObject(0).getString(KEY_STATE);
            String venue1 = data.getJSONObject(0).getString(KEY_VENUE);
            String url1 = data.getJSONObject(0).getString(KEY_WEBSITE);
            String form1 = data.getJSONObject(0).getString(KEY_LINK_REGISTRATION);
            String number1 = data.getJSONObject(0).getString(KEY_CONTACT);
            String email1 = data.getJSONObject(0).getString(KEY_EMAIL);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

感谢和GT; !!