데이터를 가져 오는 동안 JSONArray를 JSONObject 오류로 변환 할 수 없습니다.

사용자 4323984

다음 json에서 json을 읽고 싶은 Android 응용 프로그램이 있습니다.

[{"name":"ABCD","country":"INDIA","twitter":"abc"},{"name":"nehasharma","country":"india","twitter":"indore"},{"name":"Arif","country":"India","twitter":"@sdf"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"bawender","country":"18","twitter":null},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"fghjj"},{"name":"je","country":"xe","twitter":"@rtttt.com\n"},{"name":"ajh","country":"eyohio","twitter":"mp"},{"name":"hasan","country":"jsjs","twitter":"snsns"},{"name":"13.3738383383","country":"38.3838383829","twitter":"location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"}]

다음은 안드로이드 코드입니다.

public class Cultural extends ListActivity {

    private ProgressDialog pDialog;


    private static String url = "http://hmkcode.appspot.com/jsonservlet";

    private static final String NAME = "name";
    private static final String COUNTRY = "country";
    private static final String TWITTER = "twitter";

    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

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

        contactList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                // getting values from selected ListItem

                String name = ((TextView)view.findViewById(R.id.name)).getText().toString();

                String country = ((TextView) view.findViewById(R.id.time)).getText().toString();
                String twitter = ((TextView) view.findViewById(R.id.venue)).getText().toString();

                Intent in = new Intent(getApplicationContext(),SingleEventActivity.class);
                in.putExtra(NAME, name);
                in.putExtra(COUNTRY, country);
                in.putExtra(TWITTER, twitter);

                startActivity(in);

            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(Cultural.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    // contacts = jsonObj.getJSONArray(NAME);

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

                        String name = c.getString(NAME);
                        String time = c.getString(COUNTRY);
                        String twitter=c.getString(TWITTER);

                        // Phone node is JSON Object

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value

                        contact.put(NAME, name);
                        contact.put(COUNTRY, time);
                        contact.put(TWITTER,twitter);

                        //contact.put(TAG_ADDRESS, address);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(Cultural.this, contactList,R.layout.list_item, new String[] { NAME, COUNTRY,TWITTER }, new int[] { R.id.name,R.id.time, R.id.venue});

            setListAdapter(adapter);
        }

    }

}

앱을 실행하려고 할 때마다 검은 색 화면이 표시되고 콘솔에 다음 정보가 표시됩니다. JSONArray cannot be convert to JSONObject error .

데이터가 다운로드되고 콘솔에서 볼 수 있습니다.

여기서 내가 어디로 잘못 가고 있습니까?

ρяσѕρєя K

JSONArray를 JSONObject 오류로 변환 할 수 없습니다.

반환 된 응답 문자열은 JSONObject 대신 JSONObject의 JSONArray 이기 때문 입니다.

따라서 jsonStrString JSONArray대신 JSONObject. 변경 :

JSONObject jsonObj = new JSONObject(jsonStr);

...에

JSONArray jsonArr = new JSONArray(jsonStr);
for (int i = 0; i < jsonArr.length(); i++) {
   JSONObject c = jsonArr.getJSONObject(i);
   .....
}

이제 모든 JSONObject를 jsonArr.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

jsonarray를 jsonobject logcat 오류로 변환 할 수 없습니다.

분류에서Dev

이 오류 org.json.JSONObject를 JSONArray로 변환 할 수 없습니다.

분류에서Dev

오류 org.json.JSONObject를 JSONArray로 변환 할 수 없습니다.

분류에서Dev

Android json 구문 분석 오류-JSONArray를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

Android json 구문 분석 오류-JSONArray를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

Android 애플리케이션에서 데이터를 가져 오기 위해 JSON 라이브러리로 작업하는 동안 예외가 발생합니다. JSONObject를 JSONarray로 변환 할 수 없다고 말합니다.

분류에서Dev

Android JSON 구문 분석 'org.json.JSONObject 유형의 데이터를 JSONArray로 변환 할 수 없음'오류

분류에서Dev

내 안드로이드 프로젝트에서 "JSONObject를 JSONArray로 변환 할 수 없습니다"와 같이 JSON을 구문 분석하는 동안 예외가 표시되는 이유를 알 수 없습니다.

분류에서Dev

java.lang.String 유형의 값 연결은 데이터베이스에서 오브젝트를 가져올 때 JSONObject로 변환 할 수 없습니다.

분류에서Dev

org.json.jsonarray를 json으로 변환 할 수 없습니다. json 데이터 GET 작업에 대해 오류가 발생했습니다.

분류에서Dev

android org.json.JSONArray를 JSONObject로 변환 할 수 없습니까?

분류에서Dev

jsonobject는 jsonarray 중첩 jsonarray android로 변환 할 수 없습니다.

분류에서Dev

Volley를 사용하여 JSONArray를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

"데이터 형식 nvarchar를 실수로 변환하는 동안 오류가 발생했습니다."라는 오류가 발생합니다.

분류에서Dev

`?`는 오류를`std :: io :: Error`로 변환 할 수 없습니다.

분류에서Dev

URLSession을 사용하여 데이터를 가져 오는 동안 "백그라운드 스레드에서 변경 사항을 게시 할 수 없습니다."

분류에서Dev

Swift : UIColor를 NSUserDefaults에 저장하는 동안 오류가 발생했습니다 (CGFloat는 Float로 변환 할 수 없음).

분류에서Dev

Swift : UIColor를 NSUserDefaults에 저장하는 동안 오류가 발생했습니다 (CGFloat는 Float로 변환 할 수 없음).

분류에서Dev

json 서비스 문자열에 대한 오류를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

json 서비스 문자열에 대한 오류를 JSONObject로 변환 할 수 없습니다.

분류에서Dev

Django 템플릿 엔진을 jinja2로 변환하는 동안 오류가 발생하여 'with'를 사용할 수 없습니다.

분류에서Dev

데이터 구문 분석 오류-문자열을 JSONObject로 변환 할 수 없습니다.

분류에서Dev

ORA-01400 오류가 발생했습니다 : NULL을 삽입 할 수 없습니다 ... rowtype의 도움으로 데이터를 삽입하는 동안

분류에서Dev

JSONObject를 JSONArray로 변환 할 수 없음-Android

분류에서Dev

org.json.JSONException의 오류 : org.json.JSONArray 유형은 JSONObject로 변환 할 수 없습니다.

분류에서Dev

TypeError : 이미지 데이터는 float로 변환 할 수 없습니다. // .png 이미지를 가져올 때

분류에서Dev

Guava 소스를 가져 오는 동안 가져 오기 javax.inject를 해결할 수 없습니다.

분류에서Dev

PHP 및 MySQL을 사용하여 데이터베이스에서 가져 오는 동안 중복 데이터를 피할 수 없습니다.

분류에서Dev

java.lang.String 유형의 Org.json.JSONException : Value array (2) 가져 오기를 JSONObject로 변환 할 수 없습니다.

Related 관련 기사

  1. 1

    jsonarray를 jsonobject logcat 오류로 변환 할 수 없습니다.

  2. 2

    이 오류 org.json.JSONObject를 JSONArray로 변환 할 수 없습니다.

  3. 3

    오류 org.json.JSONObject를 JSONArray로 변환 할 수 없습니다.

  4. 4

    Android json 구문 분석 오류-JSONArray를 JSONObject로 변환 할 수 없습니다.

  5. 5

    Android json 구문 분석 오류-JSONArray를 JSONObject로 변환 할 수 없습니다.

  6. 6

    Android 애플리케이션에서 데이터를 가져 오기 위해 JSON 라이브러리로 작업하는 동안 예외가 발생합니다. JSONObject를 JSONarray로 변환 할 수 없다고 말합니다.

  7. 7

    Android JSON 구문 분석 'org.json.JSONObject 유형의 데이터를 JSONArray로 변환 할 수 없음'오류

  8. 8

    내 안드로이드 프로젝트에서 "JSONObject를 JSONArray로 변환 할 수 없습니다"와 같이 JSON을 구문 분석하는 동안 예외가 표시되는 이유를 알 수 없습니다.

  9. 9

    java.lang.String 유형의 값 연결은 데이터베이스에서 오브젝트를 가져올 때 JSONObject로 변환 할 수 없습니다.

  10. 10

    org.json.jsonarray를 json으로 변환 할 수 없습니다. json 데이터 GET 작업에 대해 오류가 발생했습니다.

  11. 11

    android org.json.JSONArray를 JSONObject로 변환 할 수 없습니까?

  12. 12

    jsonobject는 jsonarray 중첩 jsonarray android로 변환 할 수 없습니다.

  13. 13

    Volley를 사용하여 JSONArray를 JSONObject로 변환 할 수 없습니다.

  14. 14

    "데이터 형식 nvarchar를 실수로 변환하는 동안 오류가 발생했습니다."라는 오류가 발생합니다.

  15. 15

    `?`는 오류를`std :: io :: Error`로 변환 할 수 없습니다.

  16. 16

    URLSession을 사용하여 데이터를 가져 오는 동안 "백그라운드 스레드에서 변경 사항을 게시 할 수 없습니다."

  17. 17

    Swift : UIColor를 NSUserDefaults에 저장하는 동안 오류가 발생했습니다 (CGFloat는 Float로 변환 할 수 없음).

  18. 18

    Swift : UIColor를 NSUserDefaults에 저장하는 동안 오류가 발생했습니다 (CGFloat는 Float로 변환 할 수 없음).

  19. 19

    json 서비스 문자열에 대한 오류를 JSONObject로 변환 할 수 없습니다.

  20. 20

    json 서비스 문자열에 대한 오류를 JSONObject로 변환 할 수 없습니다.

  21. 21

    Django 템플릿 엔진을 jinja2로 변환하는 동안 오류가 발생하여 'with'를 사용할 수 없습니다.

  22. 22

    데이터 구문 분석 오류-문자열을 JSONObject로 변환 할 수 없습니다.

  23. 23

    ORA-01400 오류가 발생했습니다 : NULL을 삽입 할 수 없습니다 ... rowtype의 도움으로 데이터를 삽입하는 동안

  24. 24

    JSONObject를 JSONArray로 변환 할 수 없음-Android

  25. 25

    org.json.JSONException의 오류 : org.json.JSONArray 유형은 JSONObject로 변환 할 수 없습니다.

  26. 26

    TypeError : 이미지 데이터는 float로 변환 할 수 없습니다. // .png 이미지를 가져올 때

  27. 27

    Guava 소스를 가져 오는 동안 가져 오기 javax.inject를 해결할 수 없습니다.

  28. 28

    PHP 및 MySQL을 사용하여 데이터베이스에서 가져 오는 동안 중복 데이터를 피할 수 없습니다.

  29. 29

    java.lang.String 유형의 Org.json.JSONException : Value array (2) 가져 오기를 JSONObject로 변환 할 수 없습니다.

뜨겁다태그

보관