如何获取JSONArray值

时间:2015-11-07 06:32:16

标签: android json parsing

在我的程序中,我可以获取所有文本。但是在JSON数组中我的JSOn中还有另一个具有图像URL的JSONArray。当我使用

时,我将在JSON数组中获取所有URL
<?php

class Login extends CI_Controller {

    function index()
    {
        $data['main_content'] = 'login_form';
            //$this->load->view('header2');
        $this->load->view('includes/template', $data);  
        //$this->load->view('footer');  
    }

    function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }   

    function signup()
    {
        $data['main_content'] = 'signup_form';
        $this->load->view('includes/template', $data);
    }

    function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if($this->form_validation->run() == FALSE)
        {

            $data['main_content'] = 'signup_form';
            $this->load->view('includes/template', $data);




        }

        else
        {           
            $this->load->model('membership_model');

            if($query = $this->membership_model->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template', $data);
            }
            else
            {
                $this->load->view('signup_form');           
            }
        }

    }

    function logout()
    {
        $this->session->sess_destroy();
        $this->index();
    }

}

但是当循环结束时,我的回答中什么都没有。如何将我的ImageView替换为存储在attatchment中的URL。我的代码在这里

JSONArray attachments = post.getJSONArray("itemimage");

2 个答案:

答案 0 :(得分:0)

我可以看到你的JSON

JSONException
JSONObject attachment = attachments.getJSONObject(0);

因为您正在创建对象而不存在,所以我建议您从String中提取JSONArray并执行setImage内容。

if (null != attachments && attachments.length() > 0) {
      String image= (String) attachments.get(0);
        //--- set your image here--//
}

答案 1 :(得分:0)

嗯,我迟到了2分钟......因为你为这个演示写了。

您需要的解析方法如下,您可以使用Universal Image Loader之类的框架将网址加载到您的imageView。

    private String parseaaa(String jsonString) {

    String imgUrl = "";
    try {

        JSONObject mJsonObject = new JSONObject(jsonString);
        JSONArray mArray = mJsonObject.getJSONArray("items");
        for (int i = 0; i < mArray.length(); i++) {

            JSONObject object=mArray.getJSONObject(i);
            imgUrl+=object.getString("itemimage")+"\n";
            Log.i(TAG, imgUrl);

        }

    } catch (Exception e) {
        // TODO: handle exception
    }

  return imgUrl;
}

enter image description here

BTW,只需使用JSONArray[]JSONObject之间加载内容即可加载对象...这很容易做到。