【问题标题】:how can i read JSON array data from url correctly如何正确从 url 读取 JSON 数组数据
【发布时间】:2020-08-02 03:32:00
【问题描述】:

我正在尝试使用 VOLLEY 将 json 数据从 url 解析到我的应用程序 这是我的 JSON:

{
  "code": 200,
  "status": "OK",
  "data": {
    "number": 1,
    "name": "سُورَةُ ٱلْفَاتِحَةِ",
    "englishName": "Al-Faatiha",
    "englishNameTranslation": "The Opening",
    "revelationType": "Meccan",
    "numberOfAyahs": 7,
    "ayahs": [
      {
        "number": 1,
        "text": "بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ",
        "numberInSurah": 1,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 2,
        "text": "الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ",
        "numberInSurah": 2,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 3,
        "text": "الرَّحْمَٰنِ الرَّحِيمِ",
        "numberInSurah": 3,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 4,
        "text": "مَالِكِ يَوْمِ الدِّينِ",
        "numberInSurah": 4,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 5,
        "text": "إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ",
        "numberInSurah": 5,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 6,
        "text": "اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ",
        "numberInSurah": 6,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      },
      {
        "number": 7,
        "text": "صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ",
        "numberInSurah": 7,
        "juz": 1,
        "manzil": 1,
        "page": 1,
        "ruku": 1,
        "hizbQuarter": 1,
        "sajda": false
      }
    ],
    "edition": {
      "identifier": "quran-simple",
      "language": "ar",
      "name": "Simple",
      "englishName": "Simple",
      "format": "text",
      "type": "quran",
      "direction": "rtl"
    }
  }
}

我想显示“ayahs”数组,但只显示“文本”对象 将它们放入string 并使用intent 将它们发送到另一个activity 并在textView 中显示它们 请帮帮我

这是我的解析方法

public String[] parseJSON(){

    final String[] myText=new String[6];
    String url = "http://api.alquran.cloud/v1/surah/1";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    try {
                        JSONObject jsonObject=response.getJSONObject("data");
                        JSONArray jsonArray=jsonObject.getJSONArray("ayahs");
                        for(int i=0;i==jsonArray.length();i++){

                            JSONObject aya=jsonArray.getJSONObject(i);
                            String text=aya.getString("text");

                            myText[i] =text;

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();

        }
    });
    mRequestQueue.add(request);


    return myText;
}

这是我的意图发送方法

public void onItemClick(int position) {

    //ExampleItem clickedItem = mExampleList.get(position);
    // final int sorahNumber=++position;


    Intent detailIntent = new Intent(this, DetailActivity.class);
    String[] myText = parseJSON();
    detailIntent.putExtra("text", myText);
    startActivity(detailIntent);


}

这是我的意图捕捉器

public class DetailActivity extends AppCompatActivity {

    private TextView sorah_tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);


        sorah_tv=findViewById(R.id.sorah_tv);

        Intent intent=getIntent();
        String[] text=intent.getStringArrayExtra("text") ;
        for(int i=0;i<text.length;i++){

            sorah_tv.setText(text[i]);
        }

请帮帮我

【问题讨论】:

  • 将您的 json 数据粘贴到您的问题中。
  • 完成,感谢您通知我
  • 能否获取数组中的数据?

标签: java android json parsing android-volley


【解决方案1】:

我的猜测是循环是错误的,而不是这个:

for(int i=0;i==jsonArray.length();i++){

JSONObject aya=jsonArray.getJSONObject(i);
String text=aya.getString("text");

myText[i] =text;

}

这样做:

for(int i=0;i<jsonArray.length();i++){

JSONObject aya=jsonArray.getJSONObject(i);
String text=aya.getString("text");

myText[i] =text;

}

更新:

我认为您收到错误 OutOfBoundException,因为 JSON 数组中有 7 个对象,而 myText 数组的大小为 6:

所以不要这样:

final String[] myText=new String[6];

这样做:

final String[] myText=new String[7];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-23
    • 2018-10-25
    • 2021-11-04
    • 2020-09-23
    • 2015-07-25
    • 1970-01-01
    • 2014-03-25
    • 2022-01-20
    相关资源
    最近更新 更多