gpt4 book ai didi

java - 我如何在android studio中解析具有多个对象和数组的 Assets json文件?

转载 作者:行者123 更新时间:2023-12-02 13:38:27 25 4
gpt4 key购买 nike

我对处理 JSON 完全陌生,并且进行了很多搜索,但无法理解与我的 json 文件相关的解析。

{
"Category":[

{"Category_id":1,"Category_name":"Apple","Emails":["abc.nic.in"],"Epabx":[ "2740581", "2740583" ], "Category_Fax" : ["2740557"],
"Persons":[
{"Person_ID":1,"Name":"Adam","Designation":"Teacher","Office_Phone":"2740654",
"Residence_Phone":"2740643","VOIP":"11001","Address":"London","Fax":["NA"],"Ext":"NA"},

{"Person_ID":2,"Name":"Maria","Designation":"Secy","Office_Phone":"2790654",
"Residence_Phone":"2792866","VOIP":"NA","Address":"620/7","Fax":["NA"],"Ext":"NA"},

{"Person_ID":3,"Name":"John","Designation":"Secretary","Office_Phone":"2790652",
"Residence_Phone":"2563411","VOIP":"11011","Address":"5/7","Fax":["NA"],"Ext":"NA"}
]},

{"Category_id":2,"Category_name":"Mango","Emails":["abc.in","abc@gmail.com","abc.nic.in"],"Epabx":["2741523","2743524","2741525","2741528"], "Category_Fax" : ["NA"],
"Persons":[
{"Person_ID":1,"Name":"Harry","Designation":"Teacher","Office_Phone":"2740030",
"Residence_Phone":"2740534","VOIP":"10401","Address":"4/3","Fax":["2747075","2747395"],"Ext":"101"},

{"Person_ID":2,"Name":"Ray ","Designation":"Secy","Office_Phone":"2740030",
"Residence_Phone":"2770988","VOIP":"NA","Address":"233/22","Fax":["NA"],"Ext":"NA"},

{"Person_ID":3,"Name":"Sandy","Designation":"Engineer ","Office_Phone":"2741662",
"Residence_Phone":"2723280 ","VOIP":"10412 ","Address":"280/16","Fax":["2741663"],"Ext":"103 "}
]}
]
}

以下是我的 POJO 类:

CategoryModel.java

package com.example.android.directory;

import java.util.ArrayList;

/**
* Created by Android on 3/17/2017.
*/

public class CategoryModel {

private ArrayList<CategoryList>categoryList;

public static class CategoryList{

private int Category_id;
private String Category_name;
private ArrayList<String>Emails;
private ArrayList<String>Epabx;
private ArrayList<String>Category_Fax;
private ArrayList<Persons> persons;

public CategoryList(int category_id, String category_name,
ArrayList<String> emails, ArrayList<String> epabx, ArrayList<String> category_Fax,
ArrayList<Persons> persons) {
Category_id = category_id;
Category_name = category_name;
Emails = emails;
Epabx = epabx;
Category_Fax = category_Fax;
this.persons = persons;
}
public int getCategory_id() {
return Category_id;
}

public void setCategory_id(int category_id) {
Category_id = category_id;
}

public String getCategory_name() {
return Category_name;
}

public void setCategory_name(String category_name) {
Category_name = category_name;
}

public ArrayList<String> getEmails() {
return Emails;
}

public void setEmails(ArrayList<String> emails) {
Emails = emails;
}

public ArrayList<String> getEpabx() {
return Epabx;
}

public void setEpabx(ArrayList<String> epabx) {
Epabx = epabx;
}

public ArrayList<String> getCategory_Fax() {
return Category_Fax;
}

public void setCategory_Fax(ArrayList<String> category_Fax) {
Category_Fax = category_Fax;
}

public ArrayList<Persons> getPersons() {
return persons;
}

public void setPersons(ArrayList<Persons> persons) {
this.persons = persons;
}
}

public static class Persons{

private int Person_ID;
private String Name;
private String Designation;
private String Office_Phone;
private String Residence_Phone;
private String VOIP;
private String Address;
private ArrayList<String>Fax;
private String Ext;

public Persons(int person_ID, String name, String designation, String office_Phone,
String residence_Phone, String VOIP, String address, ArrayList<String> fax, String ext) {
Person_ID = person_ID;
Name = name;
Designation = designation;
Office_Phone = office_Phone;
Residence_Phone = residence_Phone;
this.VOIP = VOIP;
Address = address;
Fax = fax;
Ext = ext;
}


public int getPerson_ID() {
return Person_ID;
}

public void setPerson_ID(int person_ID) {
Person_ID = person_ID;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getDesignation() {
return Designation;
}

public void setDesignation(String designation) {
Designation = designation;
}

public String getOffice_Phone() {
return Office_Phone;
}

public void setOffice_Phone(String office_Phone) {
Office_Phone = office_Phone;
}

public String getResidence_Phone() {
return Residence_Phone;
}

public void setResidence_Phone(String residence_Phone) {
Residence_Phone = residence_Phone;
}

public String getVOIP() {
return VOIP;
}

public void setVOIP(String VOIP) {
this.VOIP = VOIP;
}

public String getAddress() {
return Address;
}

public void setAddress(String address) {
Address = address;
}

public ArrayList<String> getFax() {
return Fax;
}

public void setFax(ArrayList<String> fax) {
Fax = fax;
}

public String getExt() {
return Ext;
}

public void setExt(String ext) {
Ext = ext;
}
}
}

请帮助我!我该如何编写这个代码????我有一个 cardview Activity (作为引用,您可以看到此链接“ http://www.truiton.com/2015/03/android-cardview-example/ ”),其中我需要从“private ArrayList”中的 json 文件获取数据getDataSet() ”方法。请帮忙!!

最佳答案

试试这个代码。

try {

ArrayList<CategoryList>categoryList = new ArrayList<CategoryList>();
JSONObject jsonObject = new JSONObject("yourResopnseVariable");

JSONArray categoryArray = jsonObject.getJSONArray("Category");

for (int i = 0; i < categoryArray.length(); i++)
{
JSONObject job = categoryArray.getJSONObject(i);

int categoryId = job.getInt("Category_id");
String categoryName = job.getString("Category_name");

//this is for email array
ArrayList<String> emails = new ArrayList<>();
JSONArray emailArray = job.getJSONArray("Emails");
for (int j = 0; i < emailArray.length(); j++){
JSONObject jobE = emailArray.getJSONObject(j);
emails.add(jobE.getString(""+j));
}

//This i for Epabx array
ArrayList<String> epabx = new ArrayList<>();
JSONArray epabxArray = job.getJSONArray("Epabx");
for (int j = 0; i < epabxArray.length(); j++){
JSONObject jobE = epabxArray.getJSONObject(j);
epabx.add(jobE.getString(""+j));
}

//This i for Category_Fax array
ArrayList<String> category_Fax = new ArrayList<>();
JSONArray category_FaxJson = job.getJSONArray("Category_Fax");
for (int j = 0; i < category_FaxJson.length(); j++){
JSONObject jobE = category_FaxJson.getJSONObject(j);
category_Fax.add(jobE.getString(""+j));
}

//This i for Persons array
ArrayList<Persons> personsList = new ArrayList<>();
JSONArray personsArray = job.getJSONArray("Persons");
for (int j = 0; i < personsArray.length(); j++){
JSONObject jobIn = personsArray.getJSONObject(j);

int Person_ID = jobIn.getInt("Person_ID");
String Name = jobIn.getString("Name");
String Designation = jobIn.getString("Designation");
String Office_Phone = jobIn.getString("Office_Phone");
String Residence_Phone = jobIn.getString("Residence_Phone");
String VOIP = jobIn.getString("VOIP");
String Address = jobIn.getString("Address");

//this is for Fax array
ArrayList<String>Fax = new ArrayList<>();
JSONArray fax = jobIn.getJSONArray("Fax");
for (int k=0; k < fax.length(); k++)
{
JSONObject jobI = fax.getJSONObject(k);
Fax.add(jobI.getString(""+k));
}
String Ext = jobIn.getString("Ext");

personsList.add(new Persons(Person_ID, Name, Designation, Office_Phone, Residence_Phone,
VOIP, Address, Fax, Ext));
}

//here your Category[] value store in categoryArrayList
categoryList.add(new CategoryList(categoryId, categoryName, emailArray, epabx, category_Fax, personsList));
}

if (categoryList != null)
{
Log.i("categoryList size = ", ""+categoryArray.length());
}
} catch (JSONException e) {
e.printStackTrace();
}

了解更多详情 Check

关于java - 我如何在android studio中解析具有多个对象和数组的 Assets json文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com