无需解析JSON数据即可以任何方式存储JSONArray

时间:2019-02-20 06:50:43

标签: android realm

此json以下的内容是我从网络服务获取的简单数据

[
  {
    "section_month_name": "month one",
    "month_title": "title of month",
    "section_price": "150000",
    "section_available": true,
    "section_lessons": [
      {
        "time": "30.12",
        "media": "music",
        "course": "free",
        "title": "title 1",
        "content": "content 1",
        "file_url": "http:www.google.com/file_1.tmp"
      },
      {
        "time": "30.12",
        "media": "music",
        "free_course": true,
        "title": "title 2",
        "content": "content 2",
        "file_url": "http:www.google.com/file_1.tmp"
      }
    ],
    "lessons_count": 4
  }
]

MonthSections模式:

public class MonthSections extends RealmObject {
    @PrimaryKey
    private String id;
    private RealmList<SectionLesson> lessons;
    private String section_month_name;
    private String month_title;
    private String section_price;
    private boolean section_available;
    private int lessons_count;
    public MonthSections() {
    }

    /* setters and getters */
}

SectionLesson模式:

public class SectionLesson extends RealmObject {
    @PrimaryKey
    private String id;
    private String title;
    private String content;
    private String file_url;
    private String time;
    private String media;
    private String course;
    public SectionLesson() {
    }

    /* setters and getters */
}

我处理的json数组并将其保存到数据库中的是:

try {
    for (int index = 0; index < event.getData().length(); index++) {
        JSONObject month = event.getData().getJSONObject(index);
        RealmList<SectionLesson> sectionLessonList = new RealmList<>();
        realm.beginTransaction();
        JSONArray section_lesson = month.getJSONArray("section_lessons");
        for (int lessonIndex = 0; lessonIndex < section_lesson.length(); lessonIndex++) {
            JSONObject lesson = section_lesson.getJSONObject(lessonIndex);
            SectionLesson sectionLesson = realm.createObject(SectionLesson.class);
            //sectionLesson.setMonthId(latestId.getId());
            sectionLesson.setTitle(lesson.getString("title"));
            sectionLesson.setContent(lesson.getString("content"));
            sectionLesson.setFile_url(lesson.getString("file_url"));
            sectionLesson.setTime(lesson.getString("time"));
            sectionLesson.setMedia(lesson.getString("media"));
            sectionLesson.setCourse(lesson.getString("course"));
            sectionLessonList.add(sectionLesson);
        }
        MonthSections monthSections = realm.createObject(MonthSections.class, UUID.randomUUID().toString());
        monthSections.setSection_month_name(month.getString("section_month_name"));
        monthSections.setMonth_title(month.getString("month_title"));
        monthSections.setSection_price(month.getString("section_price"));
        monthSections.setSection_available(month.getBoolean("section_available"));
        monthSections.setLessons_count(month.getInt("lessons_count"));
        monthSections.setLessons(sectionLessonList);
        realm.commitTransaction();
    }
} catch (JSONException e) {
    e.printStackTrace();
    Log.e("Error", e.getMessage());
}

有什么简单的方法可以在不解析json数据的情况下存储此数组?

此代码为:

realm.createObjectFromJson(SectionLesson .class, json)

我收到此错误:

Could not create Json object from string
Caused by: org.json.JSONException: Value [{"section_month_name":"month 1","month_title":"title","section_price":"150000","section_available":true,"lessons_count":4,"section_lessons":[{"title":"tlt","content":"content","file_url":"http:www.google.com\/file_1.tmp","time":"30.12","media":"music","course":"free"}]}] of type org.json.JSONArray cannot be converted to JSONObject

,此代码为:

 realm.executeTransaction(realm -> realm.createAllFromJson(MonthSections.class, event.getData().toString()));

返回此错误:

JSON object doesn't have the primary key field 'id'

并且需要将关键字段设为“ id”

2 个答案:

答案 0 :(得分:0)

您尝试过createObjectFromJson()吗?

对于单个对象

realm.createObjectFromJson(SectionLesson.class, json)

对于JSONArray

createAllFromJson(SectionLesson.class, jsonArray)

答案 1 :(得分:0)

请在SectionLesson For循环中使用此代码

  

sectionLesson.setId(“” + System.currentTimeMillis());

 SectionLesson sectionLesson = realm.createObject(SectionLesson.class);
        //sectionLesson.setMonthId(latestId.getId());
        sectionLesson.setId(""+ System.currentTimeMillis());
        sectionLesson.setTitle(lesson.getString("title"));
        sectionLesson.setContent(lesson.getString("content"));
        sectionLesson.setFile_url(lesson.getString("file_url"));
        sectionLesson.setTime(lesson.getString("time"));
        sectionLesson.setMedia(lesson.getString("media"));
        sectionLesson.setCourse(lesson.getString("course"));
        sectionLessonList.add(sectionLesson);