使用 GSON 在其父级(家庭)JSON 中嵌套 JSON 输出(子级列表)

Nest JSON output (list of children) within its parent (household) JSON using GSON

目标是避免使用需要手动放置每个 key/value 对的 JSONObject() 和 JSONArray(),因此 GSON 或相关解决方案值得赞赏。

我有两个对象,即 1) 家庭,2) 具有以下属性的家庭成员 JSON(在 model/pojo class 中也相同)

家庭:

{
    "household": {
        "hh_id": "1",
        "avg_expense": "2",
        "avg_income": "2",
        "beneficiary": "3278963",
        "beneficiary_id": "2342343242342",
        "beneficiary_name": "name"

    }
}

家庭成员:

{
    "household_member": [{
        "age": "11",
        "creation_date": "2022-03-20 02:28:44",
        "disability": "1",
        "disability_oth": "",
        "disease": "1",
        "dob": "2011-03-20",
        "education": "1",
        "family_no": "1",
        "gender": "1",
        "hh_id": "1",
        "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
    }, {
        "age": "11",
        "creation_date": "2022-03-20 02:28:44",
        "disability": "1",
        "disability_oth": "",
        "disease": "1",
        "dob": "2011-03-20",
        "education": "1",
        "family_no": "1",
        "gender": "1",
        "hh_id": "1",
        "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
    }]
}

生成使用:

gson.toJson(household)
gson.toJson(household_members)

GSON 库中是否有任何辅助函数,我可以在其中定义其父项(家庭成员)下的子项(家庭成员),类似于将 JSON 数组放入对象中,如下所示:

jHHObject.put("household_member", jHouseholdMemberArray)

以下结果令人满意 JSON:

{
    "household": {
        "hh_id": "1",
        "avg_expense": "2",
        "avg_income": "2",
        "beneficiary": "3278963",
        "beneficiary_id": "2342343242342",
        "beneficiary_name": "name",

        "household_member": [{
            "age": "11",
            "creation_date": "2022-03-20 02:28:44",
            "disability": "1",
            "disability_oth": "",
            "disease": "1",
            "dob": "2011-03-20",
            "education": "1",
            "family_no": "1",
            "gender": "1",
            "hh_id": "1",
            "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
        }, {
            "age": "11",
            "creation_date": "2022-03-20 02:28:44",
            "disability": "1",
            "disability_oth": "",
            "disease": "1",
            "dob": "2011-03-20",
            "education": "1",
            "family_no": "1",
            "gender": "1",
            "hh_id": "1",
            "hh_mem_uuid": "7c828e43-b681-4e09-8cc4-279505244a24"
        }]

    }
}

型号:家用:

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey

@Entity(tableName = "household")
open class Household {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var hh_uuid: String
    var creation_date: String
    var district: String
    var district_name: String
    var tehsil: String
    var tehsil_name: String
    var uc: String
    var uc_name: String
    var rv: String
    var rv_name: String
    var enumerator: String
    var enumerator_name: String
    var household: String
    var household_name: String
    var respondent_name: String
    var beneficiary: String
    var beneficiary_name: String
    var beneficiary_cnic: String
    var cell: String
    var young_couples: String
    var couples: String
    var total_hh_members: String
    var total_hh_male_18: String
    var total_hh_female_18: String
    var total_hh_male_17: String
    var total_hh_female_17: String
    var total_hh_male_5: String
    var total_hh_female_5: String
    var avg_income: String
    var avg_expense: String
    var earning_hand: String
    var family_prefer: String
    var taken_loan: String
    var taken_loan_amount: String
    var own_livestock: String
    var cow: String
    var sheep: String
    var camel: String
    var horse: String
    var comments: String
    var version_code: String
    var version_name: String
    var status_sent: String = "0"


    constructor(

        hh_uuid: String,
        creation_date: String,
        district: String,
        district_name: String,
        tehsil: String,
        tehsil_name: String,
        uc: String,
        uc_name: String,
        rv: String,
        rv_name: String,
        enumerator: String,
        enumerator_name: String,
        household: String,
        household_name: String,
        respondent_name: String,
        beneficiary: String,
        beneficiary_name: String,
        beneficiary_cnic: String,
        cell: String,
        young_couples: String,
        couples: String,
        total_hh_members: String,
        total_hh_male_18: String,
        total_hh_female_18: String,
        total_hh_male_17: String,
        total_hh_female_17: String,
        total_hh_male_5: String,
        total_hh_female_5: String,
        avg_income: String,
        avg_expense: String,
        earning_hand: String,
        family_prefer: String,
        taken_loan: String,
        taken_loan_amount: String,
        own_livestock: String,
        cow: String,
        sheep: String,
        camel: String,
        horse: String,
        comments: String,
        version_code: String,
        version_name: String,
        status_sent: String

    ) {
        this.hh_uuid = hh_uuid
        this.creation_date = creation_date
        this.district = district
        this.district_name = district_name
        this.tehsil = tehsil
        this.tehsil_name = tehsil_name
        this.uc = uc
        this.uc_name = uc_name
        this.rv = rv
        this.rv_name = rv_name
        this.enumerator = enumerator
        this.enumerator_name = enumerator_name
        this.household = household
        this.household_name = household_name
        this.respondent_name = respondent_name
        this.beneficiary = beneficiary
        this.beneficiary_name = beneficiary_name
        this.beneficiary_cnic = beneficiary_cnic
        this.cell = cell
        this.young_couples = young_couples
        this.couples = couples
        this.total_hh_members = total_hh_members
        this.total_hh_male_18 = total_hh_male_18
        this.total_hh_female_18 = total_hh_female_18
        this.total_hh_male_17 = total_hh_male_17
        this.total_hh_female_17 = total_hh_female_17
        this.total_hh_male_5 = total_hh_male_5
        this.total_hh_female_5 = total_hh_female_5
        this.avg_income = avg_income
        this.avg_expense = avg_expense
        this.earning_hand = earning_hand
        this.family_prefer = family_prefer
        this.taken_loan = taken_loan
        this.taken_loan_amount = taken_loan_amount
        this.own_livestock = own_livestock
        this.cow = cow
        this.sheep = sheep
        this.camel = camel
        this.horse = horse
        this.comments = comments
        this.version_code = version_code
        this.version_name = version_name
        this.status_sent = status_sent

    }

}

型号:家庭成员

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "household_member")
class HouseholdMember {

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var hh_id: String
    var creation_date: String
    var household_id: String
    var hh_uuid: String
    var hh_mem_uuid: String
    var hh_member_bisp_id: String
    var hh_member_bisp_name: String
    var hh_name_ent: String
    var mem_name: String
    var dob: String
    var age: String
    var family_no: String
    var gender: String
    var marital_status: String
    var has_cnic: String
    var cnic: String
    var disability: String
    var disability_oth: String
    var disease: String
    var education: String
    var work: String
    var income: String
    var skill: String
    var skill_oth: String
    var beneficiary: String

    constructor(
        hh_id: String,
        creation_date: String,
        household_id: String,
        hh_uuid: String,
        hh_mem_uuid: String,
        hh_member_bisp_id: String,
        hh_member_bisp_name: String,
        hh_name_ent: String,
        mem_name: String,
        dob: String,
        age: String,
        family_no: String,
        gender: String,
        marital_status: String,
        has_cnic: String,
        cnic: String,
        disability: String,
        disability_oth: String,
        disease: String,
        education: String,
        work: String,
        income: String,
        skill: String,
        skill_oth: String,
        beneficiary: String

    ) {

        this.hh_id = hh_id
        this.creation_date = creation_date
        this.household_id = household_id
        this.hh_uuid = hh_uuid
        this.hh_mem_uuid = hh_mem_uuid
        this.hh_member_bisp_id = hh_member_bisp_id
        this.hh_member_bisp_name = hh_member_bisp_name
        this.hh_name_ent = hh_name_ent
        this.mem_name = mem_name
        this.dob = dob
        this.age = age
        this.family_no = family_no
        this.gender = gender
        this.marital_status = marital_status
        this.has_cnic = has_cnic
        this.cnic = cnic
        this.disability = disability
        this.disability_oth = disability_oth
        this.disease = disease
        this.education = education
        this.work = work
        this.income = income
        this.skill = skill
        this.skill_oth = skill_oth
        this.beneficiary = beneficiary


    }

}

最简单的解决方案是为您的家庭添加一个 household_members 字段 class;也许 对此也有帮助。

如果这不可能(或者不需要为 Room 创建的开销),您可以使用 Gson 首先将您的家庭 class 序列化为内存中的 JSON 文档,格式为Gson 的 JsonObject,然后将您的家庭成员也序列化为 JSON,并将他们添加到 JsonObject。之后,您可以将 JsonObject 转换为 JSON 文档字符串。

例如:

Gson gson = new Gson();
JsonObject householdJsonObject = gson.toJsonTree(household).getAsJsonObject();
JsonArray householdMembersJsonArray = gson.toJsonTree(householdMembers).getAsJsonArray();
householdJsonObject.add("household_members", householdMembersJsonArray);

// Convert the final JSON object to a JSON document string
// Any other toJson overload works as well, e.g. toJson(..., writer)
String json = gson.toJson(householdJsonObject);