从包含列表的Json对象访问值

时间:2017-03-16 10:59:11

标签: angularjs json

我希望有人也遇到过这个问题。我是这一切的新手,因此请原谅我缺乏知识。

我想访问对象列表并存储在数组中。任何人都可以帮助我如何访问价值,因为我有json喜欢: -

device = {
     "macAddress" : "00:00:00:00:00:00",
     "route" : [{"lat" : 28.35 , "lng" : 78.325},
                {"lat" : 28.35 , "lng" : 78.325},
                {"lat" : 28.35 , "lng" : 78.325},
                {"lat" : 28.35 , "lng" : 78.325} ]
} 

我想在我尝试使用device.route的数组中获取路由的值但是无法访问它。

4 个答案:

答案 0 :(得分:0)

device是一个JSON对象,它进一步包含" route" array和route数组包含更多Json对象。您可以访问:

var value=device.Route[1].lat; 

答案 1 :(得分:0)

什么意思是public class MainActivityFragment extends Fragment { //Expandable list Adapter ExpandableListAdapter listAdapter; ExpandableListView expListView; List<String> listDataHeader; HashMap<String, List<String>> listDataChild; //Users private List <User> items = new ArrayList<>(); //Obejeto a pasar User user = new User(); String password; @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_main, container, false); //Botones TextPass = (EditText) view.findViewById(R.id.IdpasswordUser); TextName = (TextView) view.findViewById(R.id.text1); // get the listview expListView = (ExpandableListView) view.findViewById(R.id.lvExp); // preparing list data prepareListData(); listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild); // setting list adapter expListView.setAdapter(listAdapter); ExpandableListView ExList =(ExpandableListView) view.findViewById(R.id.lvExp) ; //Llamamos al boton fichar fichar = (Button) view.findViewById(R.id.fichar); //Call ExpandibleList view child on pressed ExList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { ShowItem(listDataHeader.get(childPosition)); TextName.setText(listAdapter.getChild(groupPosition,childPosition).toString()); return false; } }); fichar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { password = TextPass.getText().toString(); //SuperUsuario if(key.equalsIgnoreCase("cesi.tic")&&password.equalsIgnoreCase("123456789")){ ((MainActivity) getActivity()).EditarUsuario(); fichar.setVisibility(view.INVISIBLE); TextPass.setVisibility(view.INVISIBLE); TextDNI.setVisibility(view.INVISIBLE); }else{ LeerFirebase(); } } }); return view; } public void ShowItem(String id) { // do something } } 无法访问。它非常好

&#13;
&#13;
device.route
&#13;
&#13;
&#13;

如果您想从路由数组中获取一个对象,则通过索引

访问该数组

var device = { "macAddress" : "00:00:00:00:00:00", "route" : [{"lat" : 28.35 , "lng" : 78.325}, {"lat" : 28.35 , "lng" : 78.325}, {"lat" : 28.35 , "lng" : 78.325}, {"lat" : 28.35 , "lng" : 78.325} ] } var routeArr = device.route; console.log(routeArr);

&#13;
&#13;
device.route[0]
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个。

&#13;
&#13;
var device = {
   "macAddress" : "00:00:00:00:00:00",
   "route" : [
      {"lat" : 28.35 , "lng" : 78.325},
      {"lat" : 28.35 , "lng" : 78.325},
      {"lat" : 28.35 , "lng" : 78.325},
      {"lat" : 28.35 , "lng" : 78.325} 
   ]
}

console.log(device.route);   //OR
console.log(device["route"]);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果你只想要路由的整个数组值。它可以很简单地获得 的 device.route 即可。

对于阵列中的各个索引值,您可以通过 device.route [index] 来执行此操作。

对于数组对象内的值。它可以通过 device.route [0] [“lat”] device.route [0] .lat <获得/强>

我希望这能回答你的问题。