Android Question Loading JSON file from a website & Parsing JSON data of Multiple Arrays

Shahid Saeed

Active Member
Licensed User
Longtime User
I went through all the Tutorials about usage of JSON file, and currently found only few methods, Local JSON file and remote JSON file.

My Question is the method for remote JSON files which provides a way through connecting to MySQL server and fetch the data and parse into JSON. But what if someone doesn't have access over to MySQL and the only source he have is the JSON file link e.g (http://www.somewebsite.com/JSON/JSON.php). What will be the method to include that JSON file, or if there is a way to read data from remote file and store into Assets folder as local JSON file.

My Second Question is about Parsing specific JSON Data from Multiple Arrays

for example :

B4X:
{
   "news_sections": [
      {
         "section_title": "الأخبار",
         "news": [
            {
               "news_date": "الأربعاء ، ١٩ مارس  ٢٠١٤",
               "news_title": "الوارية تنتزع سيف صاحب السمو رئيس الدولة",
               "news_summary": "أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله،",
               "news": "<p align=\"right\">أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، مؤكداً أن فوز هجن الرئاسة بـ «ناموس القائد»، يمثل أعلى وأروع تكريم لمسيرة إنجازاتها المتلاحقة، التي توجتها أمس بتتويج مستحق واستثنائي، مشيراً إلى أن ما شهده المهرجان الختامي بالوثبة، يمثل شاهداً حياً على دعم وعطاء القيادة للرياضات التراثية، وفي مقدمتها سباقات الهجن التي تعيش عصرها الذهبي. <\/p><p align=\"right\">كما أهدى معاليه إنجاز «هجن الرئاسة» إلى الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة، مشيداً بدعم سموه المتواصل للرياضة والرياضيين، وللرياضات التراثية بصفة خاصة، وفي مقدمتها سباقات الهجن، التي يحفظ منتسبوها لسموه أياديه البيضاء على هذه الرياضة، والتي تمثلت في عدة سباقات تطوف إمارات الدولة باسم سموه، إضافة إلى الدعم الدائم والسخي للسباقات في كل الميادين، ما جعل الإمارات وجهة للملاك والمضمرين من دول الخليج كافة. <\/p><p align=\"right\">وثمن معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، دعم وعطاء سمو الشيخ منصور بن زايد آل نهيان، نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مؤكداً أن عطاء سموه بلا حدود، وأن ما شهدته سباقات الهجن ومختلف الرياضات التراثية من تطور، كان نتاجاً لأفكار سموه الوثابة التي لا تحدها حدود، وتنطلق من حب عميق للوطن، ومن ربط رائع بين الماضي بموروثه العريق والحاضر بمعطيات التطور، تطلعاً إلى مستقبل حافل بإذن الله لدولتنا الناهضة. <br><\/p>",
               "image": "http://www.prcamels.ae/images/news/npic18711720.jpg"
            },
            {
               "news_date": "الأحد ، ١٦ مارس  ٢٠١٤",
               "news_title": "الخفيفة لـ «هجن الرئاسة» تتصدر منافسات الشيوخ للثنايا",
               "news_summary": "شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي، والتي خصصت لسن (الثنايا) الطفح ولمسافة 6 كلم، كما شهد المنافسات، جمهور غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم من دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وتصدرت (الخفيفة) لهجن الرئاسة مع المضمر مبارك أحمد مطر الخييلي الشوط الأول والمخصص للبكار المحليات وأحرزت المركز الأول والناموس بعد أن قطعت مسافة الشوط في توقيت (9:15:2 دقيقة)، وجاءت في المركز الثاني (احتلال) لهجن الرئاسة والمضمر عيسى أحمد مطر الخييلي بتوقيت وقدره(9:17:2)، وحلت ثالثة (الوجبة) لهجن الشحانية والمضمر سالم فاران المري بتوقيت (9:20:00).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic27449365.jpg"
            },
            {
               "news_date": "السبت ، ١٥ مارس  ٢٠١٤",
               "news_title": "هجن الرئاسة.. «خط أحمر»",
               "news_summary": "شهد سمو الشيخ حمدان بن محمد بن راشد آل مكتوم ولي عهد دبي، منافسات اليوم الخامس للمهرجان الختامي السنوي لسباقات الهجن في منطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد سمو الشيخ حمدان بن محمد بن راشد آل مكتوم ولي عهد دبي، منافسات اليوم الخامس للمهرجان الختامي السنوي لسباقات الهجن في منطقة الوثبة بالعاصمة أبوظبي، كما شهد المنافسات معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، والشيخ راشد بن حمدان بن محمد آل نهيان، والشيخ خليفة بن سيف بن محمد آل نهيان، والشيخ المر بن مكتوم آل مكتوم، وجمع غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم أبناء دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وكانت هجن الرئاسة قد حلقت كعادتها بناموس الإبداع وأثبتت أنها «خط أحمر» على صعيد المنافسة، بعد أن اكتسحت معظم المراكز، وغردت وحدها خارج السرب، في مكانتها التي لا تدانيها فيها غيرها، وليس أدل على ذلك من حصولها على «هاتريك» في الأشواط الرئيسية لمنافسات «الإيذاع»، التي أقيمت لمسافة 6 كلم في الميدان الجنوبي للفترة المسائية، والتي شهدت تصدر (كيف) لهجن الرئاسة والمضمر راشد محمد بالسم المنصوري منافسات الشوط الأول «أبكار مفتوح» وفازت بالمركز الأول بعد أن تصدرت المراحل الأخيرة وحققت أفضل توقيت لفئة الإيذاع بعد أن قطعت المسافة بتوقيت وقدره (8:48:5)، وجاءت في المركز الثاني (كوره) لهجن الرئاسة والمضمر راشد محمد بالسم المنصوري وقطعت المسافة بتوقيت وقدره (8:49:8)، وفي المركز الثالث (هدوء) لهجن العاصفة والمضمر حمد راشد غدير الكتبي بتوقيت وقدره (8:56:1).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic77497797.jpg"
            }
         ]
      },
      {
         "section_title": "الأخبار العامة",
         "news": [
            {
               "news_date": "الخميس ، ٣٠ يناير  ٢٠١٤",
               "news_title": "سلطان بن حمدان: دعم القيادة الرشيدة وراء تفوق هجن الإمارات",
               "news_summary": "هنأ معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة رئيس اتحاد سباقات الهجن، صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، بنجاح العملية الجراحية داعيا الله عز وجل أن يمن على سموه بالصحة والعافية، كما هنأ معاليه صاحب السمو رئيس الدولة بإنجاز هجن الرئاسة بفوز «سمحة» بالسيف الذهبي للحول و«معزز» بالخنجر الذهبي للزمول المحليات.",
               "news": "<div style=\"text-align: right;\">هنأ معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة رئيس اتحاد سباقات الهجن، صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة - حفظه الله - بنجاح العملية الجراحية داعيا الله عز وجل أن يمن على سموه بالصحة والعافية، كما هنأ معاليه صاحب السمو رئيس الدولة بإنجاز هجن الرئاسة بفوز «سمحة» بالسيف الذهبي للحول و«معزز» بالخنجر الذهبي للزمول المحليات في مهرجان أمير قطر للهجن العربية الأصيلة، وهنأ معاليه الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة بالإنجاز المميز، كما هنأ معاليه سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة بتفوق هجن الرئاسة في ميدان الشحانية، وتوجه معاليه بالتهنئة إلى شعب الإمارات بهذا الإنجاز الكبير.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">&nbsp;وقال معاليه: «هذه الإنجازات هي نتاج للدعم غير المحدود الذي تجده رياضات الآباء والأجداد عامة وسباقات الهجن على وجه الخصوص، من سموه التي تهدف إلى تعزيز قيم الماضي في نفوس الأبناء والأجيال المقبلة، مشيراً معاليه إلى أن هذا الدعم السخي كفل لهجن الرئاسة تحقيق الانتصارات والتتويج بأرفع الألقاب في السباقات التي تشارك فيها داخل وخارج الدولة. وأكد معاليه أن الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة يقدم الدعم والاهتمام الكبيرين لسباقات الهجن مما وضعها على طريق التوهج المستمر وفتح الطريق لحصد الإنجازات والتوشح بالألقاب وانعكس ذلك على هجن الرئاسة والتي تخوض التحديات بحثاً عن القمة في كافة المحافل الداخلية والخارجية.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وأوضح معاليه أن دعم ومتابعة سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة لسباقات الهجن وتوفير كافة المتطلبات ظل يصب في اتجاه تطويرها والوصول بها إلى أعلى المراتب، وما إنجاز هجن الرئاسة في مهرجان أمير قطر إلا محصلة ونتاج طبيعي للدعم الكبير الذي يقدمه سموه لرياضة سباقات الهجن.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وأوضح معاليه «سباقات الهجن في الإمارات محظوظة بالاهتمام الكبير والدعم الذي تجده من القيادة الرشيدة التي تحرص على غرس هذا الإرث التاريخي في نفوس الشباب لتواصل رياضة الآباء والأجداد مسيرتها مع الأجيال والتي تحمل العديد من المعاني العميقة في نفوس الجميع».<\/div><div style=\"text-align: right;\">وأشار معاليه إلى أن هجن الرئاسة تخوض كل التحديات للتتويج بالناموس في السباقات التي تقام محلياً أو في المنافسات الخارجية لذا تأتي مشاركاتها مليئة بالتحدي بحثاً عن التتويج، وهنا نجد أن تتويجها بسيف أمير قطر في الشحانية وبستة رموز أخرى هو امتداد لإنجازات متتالية ظلت تحققها على الدوام، ونأمل أن تتواصل مسيرة نجاحاتها في كل السباقات المقبلة.<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic89046723.jpg"
            },
            {
               "news_date": "الأحد ، ٠٨ ديسمبر  ٢٠١٣",
               "news_title": "سمو الشيخ منصور بن زايد آل نهيان يحضر مأدبة غداء أقامها معالي الشيخ سلطان بن حمدان آل نهيان",
               "news_summary": "حضر سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مأدبة غداء أقامها لسموه معالي الشيخ سلطان بن حمدان بن محمد آل نهيان، مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن.",
               "news": "<div style=\"text-align: right;\">حضر سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مأدبة غداء أقامها لسموه معالي الشيخ سلطان بن حمدان بن محمد آل نهيان، مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن. حضر المأدبة الشيخ سعيد بن حمدان بن محمد آل نهيان، والشيخ خليفة بن سلطان بن حمدان آل نهيان، وعدد من الملاك من أبناء الدولة ودول مجلس التعاون الخليجي، وقدم الحضور التهنئة لسموه بمناسبة فوز إسطبلات الوثبة بكأس اليوم الوطني للقدرة، بعد إحراز الفارس علي يوسف الحمادي المركز الأول على ظهر الفرس (آرمور).<\/div>",
               "image": "http://www.prcamels.ae/images/news/npic92061215.jpg"
            }
         ]
      },
      {
         "section_title": "السباقات",
         "news": [
            {
               "news_date": "الأربعاء ، ١٩ مارس  ٢٠١٤",
               "news_title": "الوارية تنتزع سيف صاحب السمو رئيس الدولة",
               "news_summary": "أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله،",
               "news": "<p align=\"right\">أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، مؤكداً أن فوز هجن الرئاسة بـ «ناموس القائد»، يمثل أعلى وأروع تكريم لمسيرة إنجازاتها المتلاحقة، التي توجتها أمس بتتويج مستحق واستثنائي، مشيراً إلى أن ما شهده المهرجان الختامي بالوثبة، يمثل شاهداً حياً على دعم وعطاء القيادة للرياضات التراثية، وفي مقدمتها سباقات الهجن التي تعيش عصرها الذهبي. <\/p><p align=\"right\">كما أهدى معاليه إنجاز «هجن الرئاسة» إلى الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة، مشيداً بدعم سموه المتواصل للرياضة والرياضيين، وللرياضات التراثية بصفة خاصة، وفي مقدمتها سباقات الهجن، التي يحفظ منتسبوها لسموه أياديه البيضاء على هذه الرياضة، والتي تمثلت في عدة سباقات تطوف إمارات الدولة باسم سموه، إضافة إلى الدعم الدائم والسخي للسباقات في كل الميادين، ما جعل الإمارات وجهة للملاك والمضمرين من دول الخليج كافة. <\/p><p align=\"right\">وثمن معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، دعم وعطاء سمو الشيخ منصور بن زايد آل نهيان، نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مؤكداً أن عطاء سموه بلا حدود، وأن ما شهدته سباقات الهجن ومختلف الرياضات التراثية من تطور، كان نتاجاً لأفكار سموه الوثابة التي لا تحدها حدود، وتنطلق من حب عميق للوطن، ومن ربط رائع بين الماضي بموروثه العريق والحاضر بمعطيات التطور، تطلعاً إلى مستقبل حافل بإذن الله لدولتنا الناهضة. <br><\/p>",
               "image": "http://www.prcamels.ae/images/news/npic18711720.jpg"
            },
            {
               "news_date": "الأحد ، ١٦ مارس  ٢٠١٤",
               "news_title": "الخفيفة لـ «هجن الرئاسة» تتصدر منافسات الشيوخ للثنايا",
               "news_summary": "شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي، والتي خصصت لسن (الثنايا) الطفح ولمسافة 6 كلم، كما شهد المنافسات، جمهور غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم من دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وتصدرت (الخفيفة) لهجن الرئاسة مع المضمر مبارك أحمد مطر الخييلي الشوط الأول والمخصص للبكار المحليات وأحرزت المركز الأول والناموس بعد أن قطعت مسافة الشوط في توقيت (9:15:2 دقيقة)، وجاءت في المركز الثاني (احتلال) لهجن الرئاسة والمضمر عيسى أحمد مطر الخييلي بتوقيت وقدره(9:17:2)، وحلت ثالثة (الوجبة) لهجن الشحانية والمضمر سالم فاران المري بتوقيت (9:20:00).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic27449365.jpg"
            }
         ]
      }
   ]
}

I want to load data of the first array section from this JSON code and display all arrays within that array plus there is a link to image file which I want to display as well. what will be the best approach to display that data along with the picture, please note that the data will be displayed within a scroll view or advice the best possible way to show the data other than scroll view because there are other elements on that page and only half of the acclivity size is available for this data.

The following data will be displayed from the JSON array:

News_Date
News_Title
News_Summary

I want to cover 20% of the Area with Picture and 80% with the Text in A Row and for each item the rows will be repeated.
 

eps

Expert
Licensed User
Longtime User
Please generate this with Trace.

Execute and then B4A will tell you the line that the error is occurring on.

It looks like the code is expecting a double, but you've given it "news" which is obviously a string.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
NewsJson is correct, as it is getting final results from the from the Job Done.

B4X:
If Job.Success = True Then
        NewsJson = Job.GetString
        Dim JSON As JSONParser
       'Dim Map1 As Map
       JSON.Initialize(NewsJson)

NewsUrl is used here

B4X:
Dim GetPage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Have you read this? http://www.b4x.com/android/forum/threads/android-json-tutorial.6923/

Specifically this bit
B4X:
Dim m As Map'helper map for navigatingDim MenuItems AsList
 m = Map1.Get("menu")
 m = m.Get("popup")
 MenuItems = m.Get("menuitem")
For i = 0 To MenuItems.Size - 1
 m = MenuItems.Get(i)
Log(m.Get("value"))
Next

You need to Map to the items, then pull them out using a numeric index, your code doesn't seem to do this.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
Have you read this? http://www.b4x.com/android/forum/threads/android-json-tutorial.6923/

Specifically this bit
B4X:
Dim m As Map'helper map for navigatingDim MenuItems AsList
m = Map1.Get("menu")
m = m.Get("popup")
MenuItems = m.Get("menuitem")
For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Log(m.Get("value"))
Next

You need to Map to the items, then pull them out using a numeric index, your code doesn't seem to do this.
Yes I did and maybe I am not understanding something and getting the errors.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

eps

Expert
Licensed User
Longtime User
B4X:
m = Map1.Get("menu")

MenuItems = m.Get("menuitem")

For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Next

What's not to understand?

Get the menu into m

Get MenuItems from m

Iterate through MenuItems
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
B4X:
m = Map1.Get("menu")

MenuItems = m.Get("menuitem")

For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Next

What's not to understand?

Get the menu into m

Get MenuItems from m

Iterate through MenuItems

I am loading and array which is part of another array. So according to this tutorial I have to come through the root until the array i wanted. The JasonPaser code is generated through JasonTree tool. So here I don't know how to tackle this code the example you are talking about belongs to a single array.

This is the JSON you are talking about:

B4X:
{"menu": {"id": "file","value": "File","popup": {"menuitem": [
 {"value": "New", "onclick": "CreateNewDoc()"},
 {"value": "Open", "onclick": "OpenDoc()"},
 {"value": "Close", "onclick": "CloseDoc()"}
 ]
 }
}}

This is the JSON I am dealing with:

B4X:
{"news_sections": [
 {"section_title": "title","news": [
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 },
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 }
 ]
 },
 {"section_title": "title","news": [
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 },
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 }
 ]
 },
 {"section_title": "title","news": [
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 },
 {"news_date": "news date","news_title": "news title","news_summary": "news summary","news": "News Data","image": "image url"
 }
 ]
 }
 ]
}

Please guide me where should I put this

B4X:
m = Map1.Get("menu")

MenuItems = m.Get("menuitem")

For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Next
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
You have declared a string, but the code is expecting a double(number)

B4X:
Dim news As String = colnews.Get("news")
I've just tested the following with @Erel's JSONTree http://www.prcamels.ae/json/news.json.php and everything is working perfect. All you have to do is use @Erel's simple code and add loops and calls as you require them.

@Erel's code works perfect, so you shouldn't have to do much to it.
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim news_sections As List = root.Get("news_sections")
For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next
Next

Untitled-1.jpg
 
Last edited:
  • Like
Reactions: eps
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I just tried another JSON code and you know what It worked correctly so I am sure the problem is somewhere retriving date from array of anotherarray

This code works without any issue:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim NewsUrl As String
    NewsUrl = "http://www.prcamels.ae/json/transfer.json.php"
    Dim NewsJson As String

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("TestNews")
    Dim GetPage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
    ProgressDialogShow2("يرجى الانتظار جاري التحميل ...", False)   
       
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        NewsJson = Job.GetString
       
    Dim parser As JSONParser
    parser.Initialize(NewsJson)
    Dim root As Map = parser.NextObject
    Dim title As String = root.Get("title")
    Dim pages As List = root.Get("pages")
    For Each colpages As Map In pages
    Dim price As String = colpages.Get("price")
    Dim owner_name As String = colpages.Get("owner_name")
    Dim age As String = colpages.Get("age")
    Dim mother_name As String = colpages.Get("mother_name")
    Dim camel_name As String = colpages.Get("camel_name")
    Dim gender As String = colpages.Get("gender")
    Dim purchase_date As String = colpages.Get("purchase_date")
    Dim father_name As String = colpages.Get("father_name")
    Log(owner_name)
Next
       
        'Dim GetImage As HttpJob
        'GetImage.Initialize("GetImageJob", Me)
        'GetImage.Download(news_image)
               
       
        ProgressDialogHide
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
The example has two levels. You only have one.

So you can ignore MenuItems.

B4X:
m = Map1.Get("news_sections")

For i = 0 To m.Size - 1
     news = m.Get(i)
Next

but you will have to define news in the above...
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
You have new declared at a string, but the code is expecting a double(number)

B4X:
Dim news As String = colnews.Get("news")
I've just tested the following with @Erel's JSONTree http://www.prcamels.ae/json/news.json.php and everything is working perfect. All you have to do is use @Erel's simply code and add loops and calls as you require them.

@Erel 's code works perfect, so you shouldn't have to do much to it.
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim news_sections As List = root.Get("news_sections")
For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next
Next

View attachment 24002
I am using the same code from JSONTree but getting the error.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
The example has two levels. You only have one.

So you can ignore MenuItems.

B4X:
m = Map1.Get("news_sections")

For i = 0 To m.Size - 1
     news = m.Get(i)
Next

but you will have to define news in the above...

There are Three News Sections in that array and each contains Many News in another array. What I need is to just get News Array of First Section and display it into a scroll view.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
Yes, but there is only one level. The example you quoted had two levels...

Can you just correct this for me?

B4X:
Dim JSON As JSONParser
       'Dim Map1 As Map
       JSON.Initialize(NewsJson)
       Dim root As Map = JSON.NextObject
        Dim news_sections As List = root.Get("news_sections")
        For Each colnews_sections As Map In news_sections
        Dim section_title As String = colnews_sections.Get("section_title")
        Dim news As List = news_sections.Get("news")
        For Each Colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news_news As String = colnews.Get("news")
        Dim news_image As String = colnews.Get("image")
        Dim news_title As String= colnews.Get("news_title")
        Log(news_title)
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
You have declared a string, but the code is expecting a double(number)

B4X:
Dim news As String = colnews.Get("news")
I've just tested the following with @Erel's JSONTree http://www.prcamels.ae/json/news.json.php and everything is working perfect. All you have to do is use @Erel's simply code and add loops and calls as you require them.

@Erel 's code works perfect, so you shouldn't have to do much to it.
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim news_sections As List = root.Get("news_sections")
For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next
Next

View attachment 24002

Can you help me with looping the date?
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
looping the date?
I'm presuming that the date is in standard date format? I can't read the response, sorry.

I presume that you want to loop through the date for a certain article, what do you want to do with the dates?

There are 3 sections arrays; each section arrays have news in another array. I just want to load array data of first section and display all array news in a scroll view. The Display items will comprise only 4 things news_date, news_title, news_summary and image. This is what I wanted to achieve at the moment
 
Upvote 0
Top