在android中获取没有param名称的json对象

时间:2013-05-20 09:23:16

标签: android json

我有这样的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);
                }

感谢所有人:)

3 个答案:

答案 0 :(得分:3)

尝试

//result from server
JSONObject obj=json object
//get list of keys
Iterator<String> keys=obj.keys();
while(keys.hasNext())
{
String key=keys.next();
String value=obj.getString(key);
}

答案 1 :(得分:0)

你应该使用类似Gson的东西为你完成所有的工作,你只需要创建具有相同名称变量的类,如果它是一个数组,只需要生成List&lt;您的对象名称&gt;。

以下是链接:here

答案 2 :(得分:0)

这是我的实现:

for(int i=0;i<JsonObject.length();i++)
    {
        try {
            Log.d(TAG2,"Data Value Name "+JsonObject.names().get(i));
        } catch (JSONException e)
        {
            e.printStackTrace();``
        }
    }