0

The bottom line is that I have a working code. All perfectly. I receive data without any problem, but I can not figure out how to get the data from the array of arrays. Here JSON code. Help me, please)

{  
   "timestamp":1470642450,
   "type":"news",
   "rows":[  
      {  
         "categorytitle":"Происшествия",
         "dateline":"1470641760",
         "media":[  
            {  
               "width":"200",
               "filename":"https://www.****.ru/media/8c/8c/bomba(2)__79cnk5i.jpg",
               "height":"133"
            }
         ],
         "text":"<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div>\r\n<div dir="         auto">
На Дону 6 августа был обнаружен склад боеприпасов Великой Отечественной войны.  
Находка была обнаружена около 17:20         в заброшенном саду хутора Демидовка,
         Алексеевского сельского поселения (Матвеево-Курганский район ) 
.<br /><br /><em> 
"При проведении земляных работ при корчевке деревьев были найдены 123 снаряда времен Великой Отечественной войны 80 и 75 калибра"
</em>,
         <em> <span>&ndash; </span></em>сообщает пресс-служба ДПЧС по Ростовской области.<br /><br />Сейчас место обнаружения находится под охраной сотрудников полиции. Подана заявка на разминирование.</div>\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n</div> 
\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n</div>\r\n<div>\r\n<div>\r\n<div> 
\r\n<div><a><img src="filesystem:         https://*****.org/temporary/702229962_26779_5859320227133863146.jpg" />
</a><a><span data-content="t1"></span></a><a><em></em></a></div>\r\n</div>\r\n</div>\r\n</div>",
         "link":"https://www.*****.ru/news/401647/",
         "username":"Татьяна Карпухина",
         "categoryid":"7",
         "title":"На Дону в заброшенном саду нашли склад боеприпасов времен ВОВ",
         "anouns":"В Ростовской области при проведении земляных работ в заброшенном саду были найдены 123 снаряда Великой Отечественной войны",
         "newsid":"401647"
      },
      {  
         "categorytitle":"Происшествия",
         "dateline":"1470641040",
         "media":[  
            {  
               "width":"200",
               "filename":"https://www.****.ru/media/b9/b9/antiterror_b05__0.jpg",
               "height":"133"
            }
         ],
         "text":"<p lang="         ru-RU">В Дагестане в селе Айваки Гергебильского района нашли два предмета,
         похожие на взрывные устройства. 
</p>\r\n<p lang="ru-RU">Полицейские обнаружили подозрительные предметы в частном доме,
         сообщает  
<a href="http://tass.ru/proisshestviya/3518193" target="_blank">ТАСС</a>.</p>\r\n<p lang="ru-RU">На месте работают взрывотехники МВД и ФСБ.</p>",
         "link":"https://www.****.ru/news/401646/",
         "username":"Маша Волобуева",
         "categoryid":"7",
         "title":"В Дагестане в частном доме нашли похожие на взрывчатку предметы",
         "anouns":"На месте работают взрывотехники МВД и ФСБ",
         "newsid":"401646"
      },

   ]

And some Activity code. (I want get URL in rows-media-filename.)

protected Void doInBackground(Void...params) {
     // Create an array
     arraylist = new ArrayList < HashMap < String, String >> ();
     // Retrieve JSON Objects from the given URL address
     jsonobject = JSONfunctions
         .getJSONfromURL("http://www.****.ru/cgi-bin/kernel.cgi?module=news&act=get_json&type=news&json_response=1&media=1");


     try {
         // Locate the array name in JSON
         jsonarray = jsonobject.getJSONArray("rows");
         for (int i = 0; i < jsonarray.length(); i++) {
             HashMap < String, String > map = new HashMap < String, String > ();
             jsonobject = jsonarray.getJSONObject(i);

             map.put("title", jsonobject.getString("title"));
             map.put("text", jsonobject.getString("text"));
             // Set the JSON Objects into the array
             arraylist.add(map);
         }
     } catch (JSONException e) {
         Log.e("Error", e.getMessage());
         e.printStackTrace();
     }
     return null;
 }
1

2 Answers 2

0

You can get the values of media which is present inside rows using the below code.

JSONObject jsonObject = (JSONObject) obj;
    JSONArray result = (JSONArray) jsonObject.get("rows");
    for (int i = 0; i < result.size(); i++) {
        JSONObject jsonObject1 = (JSONObject) result.get(i);
        JSONArray jsonarray1 = (JSONArray) jsonObject1.get("media");
        for (int j = 0; j < jsonarray1.size(); j++) {
            System.out.println(((JSONObject) jsonarray1.get(j)).get(
                    "height").toString());
            System.out.println(((JSONObject) jsonarray1.get(j)).get(
            "width").toString());
            System.out.println(((JSONObject) jsonarray1.get(j)).get(
            "filename").toString());
        }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

I have a ImageLoader class and I need to URL to static string
instead of printing you can assign it to the string.
0

There is not a valid JSON data. Please check your josn before parsing. If JSON is valid then parse by key-value pair. For check JSON by through JSON Validity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.