如何从数组中的数组中检索数据 [英] How to retrieve data from arrays inside arrays

查看:116
本文介绍了如何从数组中的数组中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个 json 问题是,html_instructions在数组steps里面的数组steps中,它是数组routes中的数组steps,如何获得html_instructions?

Json的某些部分:

  {
geocoded_waypoints:[],
routes :[
{
界限:{
东北:{
lat:39.4798194,
lng:-8.529889899999999
},
西南:{
lat:39.2591911,
lng:-8.7332564
}
},
版权所有:Dados do mapa©2017 Google,Inst。Geogr。Nacional,
双腿:[
{
距离:{
文本:44.6公里,
值:44551
},
持续时间:
文本:30分钟,
值:1818
},
end_address:葡萄牙托雷斯诺瓦斯,
end_location:{
lat:39.47908200000001,
lng:-8.5401204
},
start_address:葡萄牙圣塔伦,
start_location:{
lat:39.2846595,
lng:-8.7049071
},
步骤:[
{
distance:{
text:0.3 km,
value:342
},
duration:{
text:1分钟,
值:60
},
end_location:{
lat:39.2822117,
lng:-8.7036403
},
html_instructions:Siga< b> sul< / b> em
$ b points:cxwnFtdct @ JEVGLCR?PDTFNHTNNNPJPDRDN?JCHCv @ i @ VQTK`CkF
},
start_location:{
lat:39.2846595,
lng:-8.7049071
},
travel_mode:DRIVING
},
{
距离:{
文本:0.6公里,
值:605
},
持续时间:{
文本:1分钟,
值:66
},
end_location:{
lat:39.2772762,
lng:-8.7058033
},
html_instructions:Vire< ; b>àdireita< / b> b $ N $ b操作:右转,
折线:{
点:yhwnFv | bt @ NFx @ XZL \RTNt @ h @〜C`CpAbAh @ b @ d @ \h @ ^ LJLHHBLDb @ JP?JALAJAXCNCDANCLCLCPC` @ Gz @ KV @ F @ D @ JDND
},
start_location:{
lat:39.2822117,
lng:-8.7036403
},
travel_mode:DRIVING
},


这就是我从java那里得不到的:

  (jsonStr!= null){
try { .optJSONArray(routes);
if(routes!= null){
JSONArray legs = jsonObject.optJSONArray(legs);
if(legs!= null){
JSONArray direcoes = jsonObject.optJSONArray(steps);
if(direcoes!= null){
for(int j = 0; j< direcoes.length(); j ++){
JSONObject jsonObject1 = direcoes.getJSONObject(j);

String Distancia = jsonObject1.getString(distance);
String Duracao = jsonObject1.getString(duration);
字符串Instrucoes = jsonObject1.getString(html_instructions);


HashMap< String,String> direcao = new HashMap<>();

direcao.put(distance,Distancia);
direcao.put(duration,Duracao);
direcao.put(html_instructions,Instrucoes);

listaDirecoes.add(direcao);






$ b catch(最终的JSONException e){
Log.e(TAG,ERROR:+ e.getMessage());
runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(getApplicationContext(),ERROR:+ e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}

}


解决方案

你的JSON是不正确的,但如果它只是一个打字错误。
使用它。

  JSONObject root = new JSONObject(jsonStr); 
JSONArray routes = jsonObject.getJSONArray(routes);
if(routes!= bull&& route.lenght()> 0); {
JSONObject singleRoute = routes.get(0);
JSONArray legs = singleRoute.get(legs)
if(legs!= bull&& legs.lenght()> 0); {
JSONObject singleLeg = legs.get (0);
String html = singleLeg.get(html_instructions);
}

}


I'm trying to retrieve the html_instructions from this the problem is that html_instructions is inside the array "steps" which is inside the array "legs" which is inside the array "routes", how do I get the "html_instructions"

Some part of the Json:

{
  geocoded_waypoints: [],
  routes: [
    {
      bounds: {
        northeast: {
          lat: 39.4798194,
          lng: -8.529889899999999
        },
        southwest: {
          lat: 39.2591911,
          lng: -8.7332564
        }
      },
      copyrights: "Dados do mapa ©2017 Google, Inst. Geogr. Nacional",
      legs: [
        {
          distance: {
            text: "44,6 km",
            value: 44551
          },
          duration: {
            text: "30 min.",
            value: 1818
          },
          end_address: "Torres Novas, Portugal",
          end_location: {
            lat: 39.47908200000001,
            lng: -8.5401204
          },
          start_address: "Santarém, Portugal",
          start_location: {
            lat: 39.2846595,
            lng: -8.7049071
          },
          steps: [
            {
              distance: {
              text: "0,3 km",
              value: 342
            },
          duration: {
            text: "1 min.",
            value: 60
          },
          end_location: {
            lat: 39.2822117,
            lng: -8.7036403
          },
          html_instructions: "Siga <b>sul</b> em direção a <b>N3</b>",
          polyline: {
            points: "cxwnFtdct@JEVGLCR?PDTFNHTNNNPJPDRDN?JCHCv@i@VQTK`CkF"
          },
          start_location: {
            lat: 39.2846595,
            lng: -8.7049071
          },
          travel_mode: "DRIVING"
        },
        {
          distance: {
            text: "0,6 km",
            value: 605
          },
          duration: {
            text: "1 min.",
            value: 66
          },
          end_location: {
            lat: 39.2772762,
          lng: -8.7058033
          },
          html_instructions: "Vire <b>à direita</b> em direção a <b>N3</b>",
          maneuver: "turn-right",
          polyline: {
            points: "yhwnFv|bt@NFx@XZL\RTNt@h@~C`CpAbAh@b@d@\h@^LJLHHBLDb@JP?JALAJAXCNCDANCLCLCPC`@Gz@KV@F@D@JDND"
          },
          start_location: {
            lat: 39.2822117,
            lng: -8.7036403
          },
        travel_mode: "DRIVING"
      },

This is what I have from java, which is not working:

if (jsonStr != null) {
                try {
                    //JSONArray array = new JSONArray(jsonStr);
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    JSONArray routes = jsonObject.optJSONArray("routes");
                    if (routes != null) {
                        JSONArray legs = jsonObject.optJSONArray("legs");
                        if (legs != null){
                            JSONArray direcoes = jsonObject.optJSONArray("steps");
                            if (direcoes != null) {
                                for (int j = 0; j < direcoes.length(); j++) {
                                    JSONObject jsonObject1 = direcoes.getJSONObject(j);

                                    String Distancia = jsonObject1.getString("distance");
                                    String Duracao = jsonObject1.getString("duration");
                                    String Instrucoes = jsonObject1.getString("html_instructions");


                                    HashMap<String, String> direcao = new HashMap<>();

                                    direcao.put("distance", Distancia);
                                    direcao.put("duration", Duracao);
                                    direcao.put("html_instructions", Instrucoes);

                                    listaDirecoes.add(direcao);
                                }
                            }
                        }

                    }



                } catch (final JSONException e) {
                    Log.e(TAG, "ERROR: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "ERROR: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }

            }

解决方案

You'r JSON is not correct, but if it was just a typing mistake. use this.

JSONObject root = new JSONObject(jsonStr);
JSONArray routes = jsonObject.getJSONArray("routes");
if(routes!=bull && routes.lenght()>0);{
  JSONObject singleRoute = routes.get(0);
  JSONArray legs = singleRoute.get("legs")
  if(legs!=bull && legs.lenght()>0);{
     JSONObject singleLeg = legs.get(0);
     String html = singleLeg.get("html_instructions");
  }

}

这篇关于如何从数组中的数组中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆