猿问

如何将 JSON 字符串转换为 JSON 数组列表?

我有一个字符串,我想在列表中显示它,所以我必须在数组列表中转换它

我该怎么做?

我试过这些东西


     String Result =response.body().getGetFlightAvailibilityResponse().getGetFlightAvailibilityResult().getGetFlightAvailibilityResponse().getFlightDetails();

                    try {





                        Gson gson = new Gson();

                        Type type = new TypeToken<List<Object>>() 

                        {}.getType();

                        List<String> contactList = gson.fromJson(Result, 

                        type);



                      //  JSONArray jsonArray = new JSONArray(Result);

                    } catch (Exception e) {

                        e.printStackTrace();

                    } 

这是我的字符串


[[SrNo,AirlineCode,FlightNo,FromAirportCode,ToAirportCode,DepDate,DepTime,ArrDate,ArrTime,FlightClass,FlightTime,TotalAmount,TaxAmount,Stops,ValCarrier,FromTerminal,ToTerminal,MainClass,FareBasis,AgencyCharge,FareType,AvailSeats,FlightTrack] ,[100YO,9W,346,BOM,DEL,04/10/2018,07:30,04/10/2018,09:40,O,130,3998,1798,0,9W,2,3,Y, ,0,N,0,,0$307|37|100YO],[101YO,9W,953,BOM,JAI,04/10/2018,14:35,04/10/2018,16:30,O,115 ,4550,1949,1,9W,2, 2,Y,,0,N,0,,0$307|37|101YO],[101YO,9W,788,JAI,DEL,04/10/2018,17: 00,04/10/2018,18:05,O,65,0,0,0,9W,2, 3,Y,,0,N,0,,0$307|37|101YO],[102YO,9W ,2418,BOM,JAI,04/10/2018,18:20,04/10/2018,20:10,O,110,4550,1949,1,9W,2,2,Y,,0,N, 0,,0$307|37|102YO],[102YO,9W,756,JAI,DEL,04/10/2018,21:15,04/10/2018,22:20,O,65,0,0, 0,9W,2, 3,Y,,0,N,0,,0$307|37|102YO],[103YO,9W,309,BOM,DEL,04/10/2018,19:00,04/10 /2018,21:15,V,135,4995,1845,0,9W,2, 3,Y,,0,N,0,,0$307|37|103YO],[104YO,9W,2374,BOM,LKO,04/10/2018,12:25,04 /10/2018,14:45,W,140,5053,2573,1,9W,2, 2,Y,,0,N,0,,0$307|37|104YO],[104YO,9W,856, LKO,DEL,04/10/2018,15:40,04/10/2018,17:00,W,80,0,0,0,9W,2,3,Y,,0,N,0,, 0$307|37|104YO],

慕娘9325324
浏览 382回答 2
2回答

函数式编程

在这里,您将获得 JSONObject,因此您可以将其转换为JSONArray jsonArray = new JSONArray(readlocationFeed);&nbsp;line with followingJSONObject jsnobject = new JSONObject(readlocationFeed);and afterJSONArray jsonArray = jsnobject.getJSONArray("locations");for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; JSONObject explrObject = jsonArray.getJSONObject(i);}您可以在 Arraylist 上添加此对象。

慕码人8056858

This will help you.JSONArray jsonArrayList = new JSONArray();&nbsp;ArrayList<String> label=new ArrayList<>();JSONArray jsonArray = new JSONArray(readlocationFeed);&nbsp;if(jsonArray.length()>0){&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<jsonArray.length();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArrayForValue = new JSONArray(jsonArray.get(i));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ////////**********For Label/Header*******///////////&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<jsonArrayForValue.length();j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label.add(jsonArrayForValue.get(j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject eachDeatail=new JSONObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<jsonArrayForValue.length();j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eachDeatail.put(label.get(j),jsonArrayForValue.get(label.get(j)).toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonArrayList.add(eachDeatail);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答