在android中读取json作为字符串对象

时间:2013-02-19 12:00:29

标签: java android json

我已经从URL读取JSON数据作为字符串对象,并将其作为字符串对象传递给我的第二个活动。如何从此字符串对象中读取值?请帮帮我。

以下是我的json:

{
    "speciality": [
        {
            "id": "1",
            "d_name": "Dr.Steven Cohen",
            "file_upload": "dr-photo.png",
            "d_address": "3838 California St. San Francisco,CA 94118",
            "d_specialty": "Eye",
            "designation": "Ophthalmologist",
            "d_city": "San Francisco",
            "d_state": "Calfornia",
            "d_zipcode": "CA94118",
            "d_phone": "018  000  000",
            "latitude": "18.815427",
            "longitude": "76.775144"
        },
        {
            "id": "2",
            "d_name": "Dr.  Hanish Patel",
            "file_upload": "hanish-patel.jpg",
            "d_address": " 160  East 56th Street New York, NY 10022 ",
            "d_specialty": "Eye",
            "designation": "Optometrist",
            "d_city": "New York",
            "d_state": "United States",
            "d_zipcode": "NY 10022",
            "d_phone": "018 000 000",
            "latitude": "40.760407",
            "longitude": "-73.968694"
        },
        {
            "id": "3",
            "d_name": " Dr. Leonard Bley MD, FACS ",
            "file_upload": "leonard-bley.jpg",
            "d_address": " 160 East 56th Street New York, NY 10022",
            "d_specialty": "Eye",
            "designation": "Ophthalmologist",
            "d_city": "New York",
            "d_state": "United States",
            "d_zipcode": "NY 10022",
            "d_phone": "018 000 000",
            "latitude": "40.760407",
            "longitude": "-73.968694"
        },
        {
            "id": "4",
            "d_name": "Dr. John Selle",
            "file_upload": "john_selle.jpg",
            "d_address": "2250 Hayes St Ste 206 San Francisco, CA 94117",
            "d_specialty": "Eye",
            "designation": "General  Practitioner",
            "d_city": "San Francisco",
            "d_state": "Calfornia",
            "d_zipcode": "CA94118",
            "d_phone": "018 000 000",
            "latitude": "37.78604",
            "longitude": "-122.457639"
        }
    ]
}

这是我的代码:

try {
    JSONObject mainObject = new JSONObject(strjson);
    //JSONObject uniObject =         mainObject.getJSONObject("speciality");
    //JSONObject  uniName = uniObject.getJSONObject("d_name");
    //JSONObject uniURL = uniObject.getJSONObject("d_address");

    JSONObject oneObject = mainObject.getJSONObject("d_name");

    Log.e("name:", "unique name" + oneObject);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

}

4 个答案:

答案 0 :(得分:0)

你的方法很好,但有一个问题;假设 d_name 是主JSONObject中的键。

您首先需要获得关键专业的价值;然后尝试获取 d_name 值。 请记住,获得专业的回应是JSONArray。

答案 1 :(得分:0)

使用此

JSONArray Array = statusObject.getJSONArray("speciality");
            for (int j = 0; j < Array.length(); j++) 
            {   
                if (Array.getJSONObject(j).has("id")) 
                {
                    String str1 = (Array.getJSONObject(j).getString("id"));
                    String str2 = (Array.getJSONObject(j).getString("d_name"));
.
.
.
.
.
}
}

答案 2 :(得分:0)

试试这个:

我希望这会对你有所帮助......

  JSONObject mainObject = new JSONObject(strjson);
 JSONArray details= mainObject .getJSONArray("speciality");
 if (details.length()!=0) {
    for (int i = 0; i < details.length(); i++) {

    String id = details.getJSONObject(i).getString("id");

        String name = details.getJSONObject(i).getString("d_name");
                            ......
                            ......
            String longitude= friends.getJSONObject(i).getString("longitude");
}
}

答案 3 :(得分:0)

试试这个......

      JSONObject mainObject = new JSONObject(strjson);

      JSONArray Array = mainObject.getJSONArray("speciality");
        for (int j = 0; j < Array.length(); j++) 
        {   
            if (Array.getJSONObject(j).has("id")) 
            {
                String str1 = (Array.getJSONObject(j).getString("id"));
                String str2 = (Array.getJSONObject(j).getString("d_name"));
              Log.e("TA","name:"+str2);
             }
        }