-3

重複の可能性:
Android でこの JSON を解析するにはどうすればよいですか?

このリンクに従ってjsonを解析していますが、混乱しているため実行できません。重複したリンクでさえ参照を提供していません。この質問を閉じることに投票したユーザーは、提供されたリンクがこれらの質問を解決するのに何の助けにもならない場合、私の質問に答えることができますか?

質問に答えられない人には、質問を編集する権利も、提供されたリンクがこれを解決するためのポイントを提供しない場合に質問を閉じる権利もないと思います。データの解析を手伝ってください。

[
{
"id": "c200",
"name": "XYZ",
"email": "xyz@gmail.com",
},
{
"id": "c201",
"name": "Johnny",
"email": "john_johnny@gmail.com",

}]

[答え]

これはクローズド ポストなので、問題の回答を投稿します。

//JSONArray
JSONArray hospital = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

     // getting JSON string from URL
    String json = jParser.makeHttpRequest(url, "GET",
            contactList);

    try {
        // Getting Array of Contacts
          hospital = new JSONArray(json);

          if (hospital != null) {

            // looping through
                for (int i = 0; i < hospital.length(); i++) {
                    JSONObject contacts = hospital.getJSONObject(i);

            // Storing each json item in variable
            String id = contacts.getString(TAG_HID);
            String name = contacts.getString(TAG_HNAME);
            String address = contacts.getString(TAG_HADDRESS);
            String phone1=contacts.getString(TAG_HPHONE1);
            String phone2=contacts.getString(TAG_HPHONE2);
            String fax=contacts.getString(TAG_HFAX);
            String email=contacts.getString(TAG_HEMAIL);
            String url=contacts.getString(TAG_URL);
            String hlongitude=contacts.getString(TAG_LONGITUDE);
            String hlatitude=contacts.getString(TAG_LATITUDE);
            String hdistance=contacts.getString(TAG_HDISTANCE);


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_HID, id);
            map.put(TAG_HNAME, name);
            map.put(TAG_HADDRESS, address);
            map.put(TAG_HPHONE1, phone1);
            map.put(TAG_HPHONE2, phone2);
            map.put(TAG_HFAX, fax);
            map.put(TAG_HEMAIL, email);
            map.put(TAG_URL, url);
            map.put(TAG_LONGITUDE, hlongitude);
            map.put(TAG_LATITUDE, hlatitude);   
            map.put(TAG_HDISTANCE, hdistance);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } 
    }
    catch (JSONException e) {
        e.printStackTrace();
    }

JSON Parser クラスのコーディング

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
public String makeHttpRequest(String url, String method,
        ArrayList<HashMap<String, String>> contactList) {

    // Making HTTP request
    try {

        // check for request method
        if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) contactList));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } 
        else if (method == "GET") 
        {
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format((List<? extends NameValuePair>) contactList, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } 
    catch (ClientProtocolException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // return JSON String
    return json;

}
}
4

1 に答える 1

0

言わないので、Android に同梱されている組み込みの org.json パーサーを使用していると思います。別のパーサーを使用している場合は、そのドキュメントを参照してください。

json を文字列としてJSONArray コンストラクターに渡します。

于 2012-08-28T03:48:09.590 に答える