JSONException:值'cityId':4,cityName:'Ahmedab​​ad'java.lang.String无法转换为JSONObject

时间:2014-10-10 11:30:36

标签: java android json

以下是代码下降。我正在尝试解析嵌套在JSONArray中的jsonarray。

JSON:

{
    "hospital": [
        {
            "hospitalId": "1",
            "hospitalName": "Sterling Hospital",
            "description": "<p>Now available in Portugese, French, German and Spanish! Very soon the tools on the website will be available in around 10+ local languages!</p>",
            "owner": "Mr. Tapan kumar Singhel",
            "email": "",
            "fax": "",
            "website": "www.bajajallianz.com",
            "hospitalAddress": "GE Plaza, Airport Road, Yerawada,   Pune411 006.",
            "phoneNumber": "020-6602 6666",
            "facilities": "Now available in Portugese, French, German and Spanish! Very soon the tools on the website will be available in around 10+ local languages!",
            "latitude": "22.312966",
            "longitude": "69.714182",
            "createDate": null,
            "flag": "3",
            "status": "enable",
            "insurance": [
                "'id':17',name: 'ICICI Lombard General Insurance Co. Ltd.'",
                "'id':18',name: 'National Insurance Co. Ltd.'"
            ],
            "cityName": [
                "'cityId':4',cityName: 'Ahmedabad'",
                "'cityId':8',cityName: 'Pune'"
            ]
        },
        {
            "hospitalId": "3",
            "hospitalName": "Apolo Hospital",
            "description": "<div style=\"float: left; width: 455px;\">Kasarvadavali Naka latitude and longitude&nbsp;</div>",
            "owner": "gopal",
            "email": "customerservice@apollomunichinsurance.com",
            "fax": "sfsdf",
            "website": "www.apoplo.in",
            "hospitalAddress": "sdsf",
            "phoneNumber": "1234 5677 78979",
            "facilities": "Kasarvadavali Naka latitude and longitude",
            "latitude": "12.936289",
            "longitude": "80.24558090",
            "createDate": null,
            "flag": "3",
            "status": "enable",
            "insurance": [
                "'id':15',name: 'ICICI Lombard General Insurance Co. Ltd.'",
                "'id':16',name: 'IFFCO Tokio General Insurance Co. Ltd.'"
            ],
            "cityName": [
                "'cityId':6',cityName: 'Baroda'",
                "'cityId':7',cityName: 'Surat'"
            ]
        }
    ]
}

Java代码:

public void setJson(String jsonstring)
    {
        try {
            JSONObject mainobject=new JSONObject(jsonstring);
            JSONArray array=new JSONArray();
            array=mainobject.getJSONArray("hospital");
            for(int i=0;i<array.length();i++)
            {


                JSONObject jobj=array.getJSONObject(i);


                JSONArray citynamearray=jobj.getJSONArray("cityName");
                JSONArray insurancerarray=jobj.getJSONArray("insurance");
                for(int j=0;j<insurancerarray.length();j++)
                {

                    for(int k=0;k<citynamearray.length();k++)
                    {
                        JSONObject jobjcity = citynamearray.getJSONObject(k);
                        JSONObject jobjinsurance=insurancerarray.getJSONObject(j);

                        if((jobjcity.getString("cityName").equals(choosenCity))&&(jobjinsurance.getString("name").equals(choosenInsurance)))
                        {
                            hospitalnames=new ArrayList<String>();
                            hospitalidslist=new ArrayList<Integer>();
                            hospitalphone=new ArrayList<String>();
                            hospitalemail=new ArrayList<String>();
                            hospitalwebsite=new ArrayList<String>();
                            hospitaladdress=new ArrayList<String>();
                            hospitalfax=new ArrayList<String>();
                            hospitalnames.add(jobj.getString("hospitalName"));
                            hospitalidslist.add(jobj.getInt("hospitalId"));
                            if(jobj.getString("phoneNumber").equals(""))
                            {
                                hospitalphone.add("Not Available");
                            }
                            else
                            {
                                hospitalphone.add(jobj.getString("phoneNumber"));
                            }
                            if(jobj.getString("email").equals(""))
                            {
                                hospitalemail.add("Not Available");
                            }
                            else
                            {
                                hospitalemail.add(jobj.getString("email"));
                            }
                            if(jobj.getString("website").equals(""))
                            {
                                hospitalwebsite.add("Not Available");
                            }
                            else
                            {
                                hospitalwebsite.add(jobj.getString("website"));
                            }
                            if(jobj.getString("hospitalAddress").equals(""))
                            {
                                hospitaladdress.add("Not Available");
                            }
                            else
                            {
                                hospitaladdress.add(jobj.getString("hospitalAddress"));
                            }
                            if(jobj.getString("fax").equals(""))
                            {
                                hospitalfax.add("Not Available");
                            }
                            else
                            {
                                hospitalfax.add(jobj.getString("fax"));
                            }

                            flag=1;
                        }



                    }

                }
            }


            if(flag==0)
            {
                hospitalnames=new ArrayList<String>();
                hospitalidslist=new ArrayList<Integer>();
                hospitalphone=new ArrayList<String>();
                hospitalemail=new ArrayList<String>();
                hospitalwebsite=new ArrayList<String>();
                hospitaladdress=new ArrayList<String>();
                hospitalfax=new ArrayList<String>();
                hospitalnames.add("No Match Found");
                hospitalidslist.add(0);
                hospitalphone.add("No Match Found");
                hospitalemail.add("No Match Found");
                hospitalwebsite.add("No Match Found");
                hospitalfax.add("No Match Found");
                hospitaladdress.add("No Match Found");

            }

请帮我处理上述代码。如果您需要更多信息,请告诉我。它不解析子阵列保险或cityName。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

这是值''cityId':6',cityName:'Baroda'“,但是在你的代码中。你让cityName有关键。 这是错的。

在将Json转换为Object之前,只需检查Json Viewer,您就会明白

如果你的Json采用这种格式,它应该已经为你工作了。

{
  "cityName": [
    {
      "cityId": 6,
      "cityName": "Baroda"
    },
    {
      "cityId": 7,
      "cityName": "Surat"
    }
  ]
}

如果您仍想以相同的方式使用json

repalce 
{"cityName":["'cityId':4',cityName: 'Ahmedabad'","'cityId':8',cityName: 'Pune'"]}
with
{"cityName":["'cityId':6,'cityName':'Baroda'","'cityId':7,'cityName':'Surat'"   ] }

replace

JSONObject jobjcity = citynamearray.getJSONObject(k);

with
JSONObject jobjcity = new JSONObject(citynamearray.getJSONString(k));

答案 1 :(得分:0)

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

public void setJson(String jsonstring) {
        try {
            JSONObject mainobject = new JSONObject(jsonstring);
            JSONArray array = mainobject.getJSONArray("hospital");
            for (int i = 0; i < array.length(); i++) {
                JSONObject jobj = array.getJSONObject(i);
                JSONArray citynamearray = jobj.getJSONArray("cityName");
                JSONArray insurancerarray = jobj.getJSONArray("insurance");
                for (int j = 0; j < insurancerarray.length(); j++) {
                    String city = citynamearray.getString(j).split(",")[1].split(":")[1].replace("'","").trim();
                    String insurance = insurancerarray.getString(j).split(",")[1].split(":")[1].replace("'","").trim();
                    String cityid = citynamearray.getString(j).split(",")[0].split(":")[1].replace("'","").trim();
                    String insuranceid = insurancerarray.getString(j).split(",")[0].split(":")[1].replace("'","").trim();

                    if ((city.equals(choosenCity)) && (insurance.equals(choosenInsurance))) {
                        hospitalnames = new ArrayList<String>();
                        hospitalidslist = new ArrayList<Integer>();
                        hospitalphone = new ArrayList<String>();
                        hospitalemail = new ArrayList<String>();
                        hospitalwebsite = new ArrayList<String>();
                        hospitaladdress = new ArrayList<String>();
                        hospitalfax = new ArrayList<String>();
                        hospitalnames.add(jobj.getString("hospitalName"));
                        hospitalidslist.add(jobj.getInt("hospitalId"));
                        if (jobj.getString("phoneNumber").equals("")) {
                            hospitalphone.add("Not Available");
                        } else {
                            hospitalphone.add(jobj.getString("phoneNumber"));
                        }
                        if (jobj.getString("email").equals("")) {
                            hospitalemail.add("Not Available");
                        } else {
                            hospitalemail.add(jobj.getString("email"));
                        }
                        if (jobj.getString("website").equals("")) {
                            hospitalwebsite.add("Not Available");
                        } else {
                            hospitalwebsite.add(jobj.getString("website"));
                        }
                        if (jobj.getString("hospitalAddress").equals("")) {
                            hospitaladdress.add("Not Available");
                        } else {
                            hospitaladdress.add(jobj.getString("hospitalAddress"));
                        }
                        if (jobj.getString("fax").equals("")) {
                            hospitalfax.add("Not Available");
                        } else {
                            hospitalfax.add(jobj.getString("fax"));
                        }

                        flag = 1;
                    }
                }

            }
        }catch (JSONException e){
            e.printStackTrace();
        }
        if (flag == 0) {
            hospitalnames = new ArrayList<String>();
            hospitalidslist = new ArrayList<Integer>();
            hospitalphone = new ArrayList<String>();
            hospitalemail = new ArrayList<String>();
            hospitalwebsite = new ArrayList<String>();
            hospitaladdress = new ArrayList<String>();
            hospitalfax = new ArrayList<String>();
            hospitalnames.add("No Match Found");
            hospitalidslist.add(0);
            hospitalphone.add("No Match Found");
            hospitalemail.add("No Match Found");
            hospitalwebsite.add("No Match Found");
            hospitalfax.add("No Match Found");
            hospitaladdress.add("No Match Found");

        }
    }