我有一个这样的json响应:
array (
'status' => 'OK',
'categories' =>
array (
'Sepeda' =>
array (
83 => 'Sepeda MTB',
'Fullbike' =>
array (
370 => 'MTB',
371 => 'Roadbike',
374 => 'Fixie',
375 => 'Sepeda Lipat',
378 => 'City Bike',
380 => 'BMX',
382 => 'Onthel',
),
84 => 'Sepeda Lipat',
'Frame' =>
array (
372 => 'MTB',
373 => 'Roadbike',
376 => 'Fixie',
384 => 'Sepeda Lipat',
379 => 'City Bike',
381 => 'BMX',
383 => 'Onthel',
),
如何从数组 "Sepeda MTB" 、 "Sepeda Lipat" 等中获取数组
如您所知,JSON 没有像往常一样的“值”或“名称”等参数名称。老实说,我的 JSON 响应不是那么短,而是在那之后的其他值。
当我使用这段代码时
JSONObject cate = json.getJSONObject("categories");
JSONObject a = cate.getJSONObject(secid);
Log.e("a", a.toString());
这个 logcat 显示这个
> 05-20 16:38:42.840: E/a(3869): {"143":"Road Bike","144":"Kids & City
> Bike","Fullbike":{"370":"MTB","378":"City
> Bike","371":"Roadbike","382":"Onthel","375":"Sepeda
> Lipat","380":"BMX","374":"Fixie"},"96":"Helm & Body
> Protection","145":"Onthel","Wheel (Hub, Rims, dll)":{"262":"Ban
> dalam","263":"Hub & Freehub","261":"Ban
> luar","265":"Spokes","360":"Quick Release","264":"Rims","348":"Wheel
> Set"},"95":"Tas Sepeda","94":"Sepatu Sepeda","138":"Part
> Generik","93":"Part lain","Outwear":{"275":"Jersey, Glove &
> Pants","280":"Tas","276":"Helm & Topi ","277":"Sepatu","278":"Body
> Protector","279":"Goggle \/ Kacamata","359":"Bandana &
> Masker"},"91":"Jersey, Shorts & Pants","Equipment &
> Tools":{"357":"Speedometer","358":"Stickers & Decals","270":"Rack &
> Panniers","269":"Bottle & Cage","268":"Lampu \/ Senter","361":"Fender
> \/ Mudguard","272":"Carrier, Hanger, & Bike
> stand","267":"Bel","271":"Pompa","266":"Gembok","273":"Tools &
> Others"},"Fork &
> Suspension":{"353":"Fork","355":"Suspension"},"83":"Sepeda
> MTB","Frame":{"383":"Onthel","379":"City Bike","384":"Sepeda
> Lipat","381":"BMX","372":"MTB","373":"Roadbike","376":"Fixie"},"Drivetrain":{"349":"Group
> Set","136":"Part Drivetrain Lain","258":"Bottom Bracket","90":"Hub &
> Free Hub","257":"Pedal, Toeclip, & Strap","259":"Rantai","254":"Front
> Derailleur (FD)","253":"Rear Derailleur (RD)","256":"Sprocket &
> Gear","88":"Crank","255":"Crank & Chainring"},"86":"Fork &
> Shock","87":"Wheel & Tire","Control & Brake":{"250":"Seatpost &
> Clamp","251":"Saddle","252":"Headset & Spacer","369":"Cable &
> Housing","245":"Brake Set","249":"Hand grip \/ Bar tape","362":"Bar
> end \/
> Tanduk","248":"Shifter","247":"Handlebar","246":"Stem"},"142":"BMX","84":"Sepeda
> Lipat","89":"Brake & Rotor","141":"Sepeda Fixie"}
我有解决方案,这是我的解决方案
JSONObject cate = json.getJSONObject("categories");
JSONObject a = cate.getJSONObject(secid);
//get list of keys
Iterator<String> keys=a.keys();
while(keys.hasNext()){
String key=keys.next();
String value=a.getString(key);
Log.e("key", key);
Log.e("value", value);
}
谢谢大家:)