JsonHelper.java 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
package com.example.testapp.Util;

import android.util.Log;

import com.example.testapp.bean.JsonPhrase;
import com.example.testapp.bean.JsonSentence;
import com.example.testapp.bean.JsonTran;
import com.example.testapp.bean.WordItem;
import com.example.testapp.database.Interpretation;
import com.example.testapp.database.Phrase;
import com.example.testapp.database.Sentence;
import com.example.testapp.database.Word;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

// JSON工具类
public class JsonHelper {

    private static final String TAG = "JsonHelper";

    // 采用Google的GSON开源框架
    public static Gson gson = new Gson();

    // 解析默认词库数据文件,并存放到数据库中
    public static void analyseDefaultAndSave(String jsonContent) {
        if (!LitePal.findAll(Word.class).isEmpty()) {
            LitePal.deleteAll(Word.class);
            LitePal.deleteAll(Interpretation.class);
            LitePal.deleteAll(Phrase.class);
            LitePal.deleteAll(Sentence.class);
        }
        // 解析的数据格式
        List<JsonSentence> jsonSentences = new ArrayList<>();
        List<JsonTran> jsonTrans = new ArrayList<>();
        List<JsonPhrase>  jsonPhrases = new ArrayList<>();
        List<WordItem> jsonWordList = gson.fromJson(jsonContent, new TypeToken<List<WordItem>>() {}.getType());
        String word1 = jsonWordList.get(0).getHeadWord();
        Log.i(TAG,word1);
        for (WordItem jsonWord : jsonWordList) {
            Log.i(TAG,jsonWord.getHeadWord());
            Word word = new Word();
            // 设置ID
            word.setWordId(jsonWord.getWordRank());
            // 设置单词
            word.setWord(jsonWord.getHeadWord());
            // 设置音标
            if (jsonWord.getContent().getWord().getContent().getUkphone() != null) {
                if (jsonWord.getContent().getWord().getContent().getUkphone().indexOf(";") == -1)
                    word.setUkPhone("[;'" +
                            ";]'" +
                            "" + jsonWord.getContent().getWord().getContent().getUkphone() + "]");
                else
                    word.setUkPhone("[" + jsonWord.getContent().getWord().getContent().getUkphone().split(";")[0] + "]");
            }
            if (jsonWord.getContent().getWord().getContent().getUsphone() != null) {
                if (jsonWord.getContent().getWord().getContent().getUsphone().indexOf(";") == -1)
                    word.setUsPhone("[" + jsonWord.getContent().getWord().getContent().getUsphone() + "]");
                else
                    word.setUsPhone("[" + jsonWord.getContent().getWord().getContent().getUsphone().split(";")[0] + "]");
            }
            // 设置图片
            if (jsonWord.getContent().getWord().getContent().getPicture() != null)
                word.setPicAddress(jsonWord.getContent().getWord().getContent().getPicture());
            // 设置巧记
            if (jsonWord.getContent().getWord().getContent().getRemMethod() != null)
                word.setRemMethod(jsonWord.getContent().getWord().getContent().getRemMethod().getVal());
            // 设置归属书目
            word.setBelongBook(jsonWord.getBookId());
            // 保存
            word.save();
            /*至此,单词的基本内容已经保存,接下来把其他表的数据保存并绑定到这个单词上*/
            // 设置短语
            if (jsonWord.getContent().getWord().getContent().getPhrase() != null) {
                jsonPhrases = jsonWord.getContent().getWord().getContent().getPhrase().getPhrases();
                for (JsonPhrase jsonPhrase : jsonPhrases) {
                    Phrase phrase = new Phrase();
                    phrase.setChsPhrase(jsonPhrase.getpCn());
                    phrase.setEnPhrase(jsonPhrase.getpContent());
                    phrase.setWordId(jsonWord.getWordRank());
                    phrase.save();
                }
            }
            // 设置释义
            jsonTrans = jsonWord.getContent().getWord().getContent().getTrans();
            for (JsonTran jsonTran : jsonTrans) {
                Interpretation interpretation = new Interpretation();
                interpretation.setWordType(jsonTran.getPos());
                interpretation.setCHSMeaning(jsonTran.getTranCn().replace(";;", ";").replace(",",","));
                interpretation.setENMeaning(jsonTran.getTranOther());
                interpretation.setWordId(jsonWord.getWordRank());
                interpretation.save();
            }
            // 设置例句
            if (jsonWord.getContent().getWord().getContent().getSentence() != null) {
                jsonSentences = jsonWord.getContent().getWord().getContent().getSentence().getSentences();
                for (JsonSentence jsonSentence : jsonSentences) {
                    Sentence sentence = new Sentence();
                    sentence.setChsSentence(jsonSentence.getsCn());
                    sentence.setEnSentence(jsonSentence.getsContent());
                    sentence.setWordId(jsonWord.getWordRank());
                    sentence.save();
                }
            }
            // 清空数据,防止重复
            jsonPhrases.clear();
            jsonSentences.clear();
            jsonTrans.clear();
        }
        Log.d(TAG, "analyseDefaultAndSave: ");
    }

}