如何将Json解析为另一个活动

时间:2014-08-01 13:52:49

标签: java android json android-intent

我有3个活动需要Json显示数据,第二个活动根据其ID从主活动中获取值,我需要将Json传递给它们中的每一个以便它可以接收数据,怎么能我通过意图将其传递给另一个活动?我对android开发很新,我已经阅读了一些与此案有关的问题,但我对此并不了解。

public void updateList() {


        feedListView= (ListView) findViewById(R.id.custom_list);
        feedListView.setVisibility(View.VISIBLE);
        progressbar.setVisibility(View.GONE);
        feedListView.setAdapter(new CustomListAdapter(this, feedList));


        feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {


                TextView clienteId = (TextView) v.findViewById(R.id.clienteId);
                System.out.println("Clicou  " + clienteId.getText());

                int idCliente = Integer.valueOf(String.valueOf(clienteId.getText()));

                for(int x=0; x<feedList.size(); x++) {


                    if(feedList.get(x).getId_rm() == idCliente) {

                        ArrayList<ClientesContatosModel> contatosDoCliente = new ArrayList<ClientesContatosModel>();

                        System.out.println(feedList.get(x).getNome() + " tem " + feedList.get(x).getContatos().size() + " contatos");

                        if(feedList.get(x).getContatos().size() > 0) {

                            System.out.println("Valor de x " + x);

                            for(int y=0; y<feedList.get(x).getContatos().size(); y++) {

                                ClientesContatosModel mClientesContatosModel = new ClientesContatosModel();

                                mClientesContatosModel.setNomeContato(feedList.get(x).getContatos().get(y).getNomeContato() );
                                mClientesContatosModel.setCargo(feedList.get(x).getContatos().get(y).getCargo());

                                contatosDoCliente.add(mClientesContatosModel);

                            }

                        }


                        Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);
                        intent.putExtra("data", contatosDoCliente);


                        startActivity(intent);

                    }

                }

            }

        });



    }

解析杰森:

public void parseJson(JSONObject json) throws JSONException {

        try {

            JSONArray dados = json.getJSONArray("dados");
            feedList = new ArrayList<ClientesModel>();
            // parsing json object
            for (int i = 0; i < dados.length(); i++) {

                JSONObject item = dados.getJSONObject(i);
                ClientesModel mClientesModel = new ClientesModel();


                mClientesModel.setId_rm(item.optInt("id"));
                mClientesModel.setNome(item.optString("nome"));
                mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
                mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
                mClientesModel.setCpf(item.optString("cpf"));
                mClientesModel.setCnpj(item.optString("cnpj"));

                feedList.add(mClientesModel);


                JSONArray contatos = item.getJSONArray("contatos");

                if (contatos != null) {

                    contatoList = new ArrayList<ClientesContatosModel>();

                    for (int j = 0; j < contatos.length(); j++) {


                        JSONObject data = contatos.getJSONObject(j);

                        ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

                        mClientesContatoModel.setId_rm(data.optInt("id_rm_cliente"));
                        mClientesContatoModel.setNomeContato(data.optString("nome"));
                        mClientesContatoModel.setCargo(data.optString("cargo"));

                        contatoList.add(mClientesContatoModel);

                        JSONArray telefone = data.getJSONArray("telefones");

                        if (telefone != null) {

                            telefoneList = new ArrayList<ContatosTelefoneModel>();

                            for (int k = 0; k < telefone.length(); k++) {


                                JSONObject tel = telefone.getJSONObject(k);

                                ContatosTelefoneModel mContatosTelefoneModel = new ContatosTelefoneModel();

                                mContatosTelefoneModel.setId_rm(tel.optInt("id_rm_telefone"));
                                mContatosTelefoneModel.setNumero(tel.optString("numero"));
                                mContatosTelefoneModel.setTipo(tel.optString("tipo"));

                                telefoneList.add(mContatosTelefoneModel);

                            }

                        }


                        mClientesModel.setContatos(contatoList);
                        mClientesContatoModel.setTelefone(telefoneList);

                        System.out.println(mClientesContatoModel);
                    }


                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

我要做的是:

intent.putExtra("json", (java.io.Serializable) new JSONArray().toString());

在我的第二项活动中:

Bundle parametros = getIntent().getExtras();

        String jsonArray = parametros.getString("json");

        try {
            JSONArray array = new JSONArray(jsonArray);

            System.out.println("json array " + array.toString(2));
        } catch (JSONException e) {
            e.printStackTrace();
        }

如何从主json获取“telefones”并在第二个活动中传递它?

2 个答案:

答案 0 :(得分:1)

您可以尝试创建单个对象来存储JSON响应,然后从您的活动中引用此对象,而不是通过Intents传递值:

public class DataObject{

    private DataObject(){} // only one instance allowed
    public JSONArray telefone;

    public set_values(){
        // set your JSON stuff here
        telefone = data.getJSONArray("telefones");        
    }

}

现在,您可以从任何活动...

中引用此对象
public class SecondActivity extends Activity{
    private JSONArray telefone;

    private void get_values(){
        this.telefone = DataObject.telefone;
    }
}

我希望这有帮助!

答案 1 :(得分:1)

<强> STEP-1

使用此代码发出HTTP请求(对于您的网址中的url传递)

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 500);

Client = new DefaultHttpClient(params);
httpget = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
mContent = Client.execute(httpget, responseHandler);

然后使用它来获取您的数据:

   JSONObject jsonObject = new JSONObject(mContent);
   String telefone = jsonObject.getJSONArray("telefones"); 

OUTPUT :: String url包含您要搜索的数据!


注意 ::

  • 对于url您必须传递您尝试的网址 获得JSON响应
  • 同样将上述代码放在Asynchronous task内 崩溃,因为你正在发出网络请求

STEP-2 :&lt; 例如::来自Activity1 &gt;

借助意图将telefone传递给新活动

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name",telefone);
startActivity(i);

STEP-3 :&lt; 例如::来自Activity2 &gt;

接收另一项活动中传递的字符串

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

希望这会有所帮助!如果您有任何错误,请退回。