json javob, android qiymatlarini oladi

Men Android uchun yangi va json obyekti qiymatlarini olish uchun kurashmoqdaman. Kimdir menga yordam berishi mumkinmi?

the json returned from the server is {"status":"active"}

Android Asynchronous Http Client kutubxonasidan foydalanmoqdaman.

public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {
                JSONArray j = new JSONArray(response);
                t.setText(j.getJSONObject(0).getString('status'));//this doesn't set the text to the status
            } catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }
        }
0
JSON ma'lumotlarining asoslarini tushunishingiz kerak. {} JSON ob'ektini ifodalaydi, [] Json majmuasini anglatadi.
qo'shib qo'ydi muallif Paresh Mayani, manba
qo'shib qo'ydi muallif Nilabja, manba
Javobimni quyida tekshiring
qo'shib qo'ydi muallif Zaki Pathan, manba

6 javoblar

foydalanishingiz mumkin

JSONObject jsonObject = new JSONObject(response);

chunki {"status": "faol"} - bu JSONObject.

va foydalanish

 t.setText(jsonObject.getString("status"));

To'liq kod

public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {
                JSONObject jsonObject = new JSONObject(response);
                t.setText(jsonObject.getString("status"));
            } catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }
        }

Boshqa javoblarga ko'ra, yangi JSONObject yaratish uchun pul sarflanadi. Shuning uchun uni bevosita foydalaning

   public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

                try {

                    t.setText(response.getString("status"));
                } catch (JSONException e) {
                    Log.e("MYAPP", "unexpected JSON exception", e);
                }
            }

Eslatma: ko'proq misollarni yaratish yomon ishlash uchundir

1
qo'shib qo'ydi
Rahmat. Men bilaman, men obj [0] ni bajargan bo'lar edim .Javascriptdagi manzil, lekin bu erda ishlamadi.
qo'shib qo'ydi muallif maxcc, manba

JSON ob'ektini kelgan parametr sifatida qabul qilish JSON array yoki JSON ob'ektining tashqi obyektini yaratishingiz shart emas Buni ko'ring.

t.setText(response.getString("status"));
1
qo'shib qo'ydi

Qiymat olish uchun to'g'ridan-to'g'ri JSONObjectdan foydalaning

 public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

       try {
            t.setText(response.getString("status"));
        } catch (JSONException e) {
            Log.e("MYAPP", "unexpected JSON exception", e);
        }
    }
0
qo'shib qo'ydi

Buni o'zgartiring:

JSONArray j = new JSONArray(response);
t.setText(j.getJSONObject(0).getString("status"));

bilan:

JSONObject j = new JSONObject(response);
t.setText(j.getString("status"));

Agar siz Stringni qabul qilsangiz, bu umidni sinab ko'rishingiz kerak

EDIT:

Ammo sizning holatlaringizda JSONObject javob yuboradi. Shunday qilib, siz to'g'ridan-to'g'ri bunday qilishingiz mumkin:

t.setText(response.getString("status"));
0
qo'shib qo'ydi
O'zgartirilgan javobimni tekshiring. t.setText (response.getString ("status") harakat qilib ko'ring); to'g'ridan-to'g'ri JSONArray o'rniga j = yangi JSONArray (javob); t.setText (j.getJSONObject (0) .getString ("status"));
qo'shib qo'ydi muallif Zaki Pathan, manba
agar bu ishni to'g'ri javob sifatida ko'rsatsa, boshqalar undan yordam olishadi. Sizga yordam berishdan baxtiyor :) :) :)
qo'shib qo'ydi muallif Zaki Pathan, manba
qaysi katalogni import qiladigan toString() ni hal qila olmaydi?
qo'shib qo'ydi muallif maxcc, manba
Rahmat. U ishladi.
qo'shib qo'ydi muallif maxcc, manba

Siz JSON obyekti va JSON array bilan tanish emasligingiz ko'rinadi. Javobingizni Log.i ("javob", response.toString ()) ustiga bosib, keyin JSON tekshiruvchisi Siz haqiqiy narsalarni amalga oshirasiz.

JSON Array va JSON obyekti da

0
qo'shib qo'ydi

GetString o'rniga optStringdan foydalaning, chunki agar getString va "status" kaliti chiqmasa, u holda app ishlamay qolishi mumkin.

public void onSuccess(int statusCode, Header[] headers, JSONObject response) {

            try {

              t.setText(j.getJSONObject(0).optString('status'));

            } catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }
        }
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb