-3

我有多级 json 数据,我想解析成 json array.but 它的抛出异常:

> -E/Error JSONException: Value [{"name":"Desktop","children_lv3":[{"name":"Dell
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_62"},{"name":"HCL
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_63"},{"name":"HP
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_64"},{"name":"IBM
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_65"},{"name":"Lenevo
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60_66"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_60"},{"name":"Laptop","children_lv3":[{"name":"Acer
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_69"},{"name":"HCL
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_70"},{"name":"HP
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_71"},{"name":"IBM
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68_72"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_68"},{"name":"Monitor","children_lv3":[{"name":"Dell
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_74"},{"name":"LG
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_75"},{"name":"Samsung
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_76"},{"name":"Soni
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_77"},{"name":"Wipro
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73_78"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_73"},{"name":"Laptop
> Accessories","children_lv3":[{"name":"Accessories
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_80"},{"name":"DVD
> Design
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_81"},{"name":"HDD
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_82"},{"name":"Keyboard
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_83"},{"name":"Mouse
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79_84"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_79"},{"name":"Desktop
> Accessories","children_lv3":[{"name":"Accessories
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_87"},{"name":"External
> Accessories
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_88"},{"name":"HDD
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_89"},{"name":"Keyboard
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_90"},{"name":"Mouse
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_91"},{"name":"Software
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86_92"}],"href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_86"},{"name":"Printer
> and Scanner","children_lv3":[{"name":"EPSON
> (0)","href":"http:\/\/opencart.codeniques.com\/shopping\/index.php?route=product\/category&path=59_93

我的 MainActivity.java:-

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

//private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
String data1,data2,data3,data4,data5;
int cnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);
    // display the first navigation drawer view on app launch
    displayView(0);
    Log.d("oncreate method","");

    new ProductsAsynTask().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$");


}

public class ProductsAsynTask extends AsyncTask<String,Void,Void>{

    ProgressDialog dialog;

    protected void onPreExecute(){
        super.onPreExecute();
        Log.d("In onPreExceute","");
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, Please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    protected Void doInBackground(String... param){
        try{
            Log.d("In doInBackground","");

            HttpClient client= new DefaultHttpClient();
            HttpPost post = new HttpPost(param[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if(status == 200){
                Log.d("Status",""+status);

                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
                JSONArray jArray = jsonObject.getJSONArray("categories");

                for(int i = 0;i < jArray.length();i++){
                    cnt++;
                    Log.d("value of array",jArray.length()+"");
                    Log.d("Value of i",""+i);

                    JSONObject jsonObject1 = jArray.getJSONObject(i);



                    data1 = jsonObject1.getString("name");
                    Log.d("hello ",data1);


                    JSONObject jsonObject2 = jsonObject1.getJSONObject("children");

                    for(int j=0;j<jsonObject2.length();j++){
                        data2 = jsonObject2.getString("name");
                        Log.d("hello2",data2);



                        JSONObject jsonObject3 = jsonObject2.getJSONObject("children_lv3");
                        for(int k=0;k<jsonObject3.length();k++){
                            data3 = jsonObject3.getString("name");
                            Log.d("hello3 ",data3);
                            data4 = jsonObject3.getString("href");
                            Log.d("hello4 ",data4);
                        }

                        data5=jsonObject2.getString("href");
                        Log.d("hello5 ",data5);
                    }


                }

            }
        }catch(IOException e){
            Log.e("Error IOException :",e.getMessage());
        }catch (JSONException e){
            Log.e("Error JSONException",e.getMessage());
        }
            return null;
    }

    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dialog.dismiss();
        Log.d("Counter value",""+cnt);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if(id == R.id.action_search){
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
    displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:

           fragment = new HomeFragment();
           title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}
}

我的 Json 数据链接在这里

4

1 回答 1

1

首先,使用http://jsonlint.com/来验证您正在解析的 json。

一旦你这样做了,你会发现就像“类别”一样,“孩子”也是一个 Json 数组而不是对象。所以,你需要使用

jsonObject1.getJSONArray("children");

希望这可以帮助

于 2015-09-30T09:22:56.173 回答