[英]how can i read JSON array data from url correctly
我正在嘗試使用 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”數組,但只有“文本”object 將它們放在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]);
}
請幫幫我
我的猜測是循環是錯誤的,而不是這個:
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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.