用多个值解析android中的JSON响应

时间:2016-10-15 15:55:43

标签: java android json

我从网页获取JSON响应作为以下字符串,我需要解析该响应并存储在android中的Java数组中。

{"companyName":["Sanmar Group","Dcygnus Technocracy"]}
{"CampusDate":["25\/10\/2016","24\/10\/2016"]}
{"companyPackage":["3.67","1.8"]}
{"status":["your are not eligible","your are not eligible"]}

2 个答案:

答案 0 :(得分:1)

这是带解析器的正确json格式

///Start Php///

var response = array('companyName'=>$companies,'CampusDate'=>$campdates,'companyPackage'=>$packages,'status'=>$status);
echo json_encode(response );

///End Php///

{
    "companyName": ["Sanmar Group", "Dcygnus Technocracy"],
    "CampusDate": ["25/10/2016", "24/10/2016"],
    "companyPackage": ["3.67", "1.8"],
    "status": ["your are not eligible", "your are not eligible"]
}

//保留公司所有价值的类

class CompanyInfo {
    public String companyName;
    public String CampusDate;
    public String companyPackage;
    public String status;
}

//Json Parser
class CompanyParser {

    public ArrayList<CompanyInfo> allComInfo;

    public CompanyParser() {
        allComInfo = new ArrayList<CompanyInfo>();
    }

    public void parseJson(String jsonString) {
        try {
            JSONObject reader = new JSONObject(jsonString);
            parseCompanyName(reader.getJSONArray("companyName"));
            parseCampusDate(reader.getJSONArray("CampusDate"));
            parseCompanyPackage(reader.getJSONArray("companyPackage"));
            parseStatus(reader.getJSONArray("status"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void checkAndAddNewEmptyCompnayAtIndex(int index) {
        if (index >= allComInfo.size()) {
            allComInfo.add(new CompanyInfo());
        }
        else if(allComInfo.get(index) == null){
            allComInfo.add(new CompanyInfo());
        }
    }

    private void parseCompanyName(JSONArray data) throws JSONException {
        int size = data.length();
        for (int ak = 0; ak < size; ak++) {
            checkAndAddNewEmptyCompnayAtIndex(ak);
            allComInfo.get(ak).companyName = data.getString(ak);
        }
    }

    private void parseCampusDate(JSONArray data) throws JSONException {
        int size = data.length();
        for (int ak = 0; ak < size; ak++) {
            checkAndAddNewEmptyCompnayAtIndex(ak);
            allComInfo.get(ak).CampusDate = data.getString(ak);
        }
    }

    private void parseCompanyPackage(JSONArray data) throws JSONException {
        int size = data.length();
        for (int ak = 0; ak < size; ak++) {
            checkAndAddNewEmptyCompnayAtIndex(ak);
            allComInfo.get(ak).companyPackage = data.getString(ak);
        }
    }

    private void parseStatus(JSONArray data) throws JSONException {
        int size = data.length();
        for (int ak = 0; ak < size; ak++) {
            checkAndAddNewEmptyCompnayAtIndex(ak);
            allComInfo.get(ak).status = data.getString(ak);
        }
    }


}

答案 1 :(得分:-2)

var response = array('companyName'=>$companies,'CampusDate'=>$campdates,'companyPackage'=>$packages,'status'=>$status);
echo json_encode(response );