-1

我有一些 JSON 数据,如下所示:

{
    "pp": {
        "status_marital_pmg": "BELUM MENIKAH",
        "nama_pmg": "IWEN LENDER",
        "kerjab_pmg": "-",
        "pendidikan_pmg": "S1",
        "agama_pmg": "KRISTEN KATOLIK",
        "bidang_industri_pmg": "JASA KEUANGAN",
        "warga_pmg": "INDONESIA",
        "hubungan_pmg_ttg": "Diri Sendiri",
        "pendanaana_pmg": "GAJI",
        "pendanaan_pmg": "GAJI",
        "usia_pmg": 33,
        "penghasilan_pmg": "> RP. 50 JUTA - RP. 100 JUTA",
        "kelamin_pmg": "Pria",
        "tujuan_pmg": "INVESTASI",
        "tujuana_pmg": "INVESTASI",
        "kerja_pmg": "KARYAWAN",
        "bidang_industria_pmg": "JASAKEUANGAN"
    }
}

我正在尝试使用以下类映射此 JSON:

public class VariabelViewer {
    private String nama_pmg;
    private String pendidikan_pmg;
    private String warga_pmg;
    private String usia_pmg;
    private String status_marital_pmg;
    private String kelamin_pmg;
    private String agama_pmg;
    private String tujuan_pmg;
    private String tujuana_pmg;
    private String penghasilan_pmg;
    private String pendanaan_pmg;
    private String pendanaana_pmg;
    private String bidang_industria_pmg;
    private String hubungan_pmg_ttg;

    public VariabelViewer(String nama_pmg, String pendidikan_pmg,
            String warga_pmg, String usia_pmg, String status_marital_pmg,
            String kelamin_pmg, String agama_pmg, String tujuan_pmg,
            String tujuana_pmg, String penghasilan_pmg, String pendanaan_pmg,
            String pendanaana_pmg, String bidang_industria_pmg,
            String hubungan_pmg_ttg) {

        this.nama_pmg = nama_pmg;
        this.pendidikan_pmg = pendidikan_pmg;
        this.warga_pmg = warga_pmg;
        this.usia_pmg = usia_pmg;
        this.status_marital_pmg = status_marital_pmg;
        this.kelamin_pmg = kelamin_pmg;
        this.agama_pmg = agama_pmg;
        this.tujuan_pmg = tujuan_pmg;
        this.tujuana_pmg = tujuana_pmg;
        this.penghasilan_pmg = penghasilan_pmg;
        this.pendanaan_pmg = pendanaan_pmg;
        this.pendanaana_pmg = pendanaana_pmg;
        this.bidang_industria_pmg = bidang_industria_pmg;
        this.hubungan_pmg_ttg = hubungan_pmg_ttg;
    }

    public String getNama_pmg() {
        return nama_pmg;
    }

    public String getPendidikan_pmg() {
        return pendidikan_pmg;
    }

    public String getWarga_pmg() {
        return warga_pmg;
    }

    public String getUsia_pmg() {
        return usia_pmg;
    }

    public String getStatus_marital_pmg() {
        return status_marital_pmg;
    }

    public String getKelamin_pmg() {
        return kelamin_pmg;
    }

    public String getAgama_pmg() {
        return agama_pmg;
    }

    public String getTujuan_pmg() {
        return tujuan_pmg;
    }

    public String getTujuana_pmg() {
        return tujuana_pmg;
    }

    public String getPenghasilan_pmg() {
        return penghasilan_pmg;
    }

    public String getPendanaan_pmg() {
        return pendanaan_pmg;
    }

    public String getPendanaana_pmg() {
        return pendanaana_pmg;
    }

    public String getBidangindustria_pmg() {
        return bidang_industria_pmg;
    }

    public String getHubungan_pmg_ttg() {
        return hubungan_pmg_ttg;
    }
}

我不知道如何在我的代码中实际声明正确的类并解析数据。我认为我的 JSON 代表一个包含另一个 JSON 数组的 JSON 数组,但我的理解可能是错误的?谁能告诉我如何解析这个?谢谢。

4

2 回答 2

2

认为我的 JSON 也是包含 JSON 数组的 JSON 数组,我的理解是否错误?

是的,你错了,因为当前的 Json String 只包含 JSONObject 而不是 JSONArray。因此,您需要首先将当前字符串转换为 JSONObject,然后从中提取所有对象:

JSONObject json=new JSONObject(<current json string>);

// get all key names from JSONObject
JSONArray allnames=json.names();

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

  JSONObject jsonobj=json.getJSONObject(allnames[i]);

  // get all values from inner JSONObject and store in any data structure 
  //get status_marital_pmg
  String str_status_marital_pmg=jsonobj.optString("status_marital_pmg");
  ....

}
于 2013-05-14T02:18:07.687 回答
1
try {
            JSONObject jsobj = new JSONObject("your string goes here");
            JSONObject ppobj = jsobj.getJSONObject("pp");
            String status_marital_pmg = ppobj.getString("status_marital_pmg");
            Integer usia_pmg = ppobj.getInt("usia_pmg");
        } catch (JSONException e) {
        }
于 2013-05-14T02:14:45.270 回答