如何在JSON对象中获取相应的JSON值?

时间:2017-05-07 23:03:21

标签: android json listview

获取name Toast onItemClick ListView code [ { "code": "123", "name": "Jim" }, { "code": "456", "name": "Sally" }, { "code": "789", "name": "Bob" }, { "code": "012", "name": "Jill" }, etc... ] 的{​​{1}}值,但我如何获得name值?

我有一个看起来像这样的JSON数组:

ListView

我在public void onStart(){ super.onStart(); // Create json array request JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST, "http://www.nameofsite.com/nameoffile.php", (JSONArray)null, new Response.Listener<JSONArray>(){ public void onResponse(JSONArray jsonArray){ // Successfully download json // So parse it and populate the listview for(int i=0;i<jsonArray.length();i++){ try { JSONObject jsonObject=jsonArray.getJSONObject(i); items.add(jsonObject.getString("name")); } catch (JSONException e) { e.printStackTrace(); } } adapter.notifyDataSetChanged(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.e("Error", "Unable to parse json array"); } }); // Create request queue RequestQueue requestQueue= Volley.newRequestQueue(this); // add json array request to the request queue requestQueue.add(jsonArrayRequest); } 中得到Toast这样的值:

name

onItemClick listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { TextView myTextView = (TextView) view.findViewById(R.id.txt); String text = myTextView.getText().toString(); Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show(); } }); 中的code像这样:

toast

但我想要name中的<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Monitor</title> <script> window.config = { API_URL: "<%= htmlWebpackPlugin.options.API_URL_TEMPLATE_VAR %>" } </script> </head> <body> <div class="container"></div> </body> </html> 值,而不是plugins: [ // Define environment variables that are accessible inside of app javascript. new webpack.DefinePlugin({ 'process.env': { 'NODE_ENV': JSON.stringify(process.env.NODE_ENV) } }), // Adds bundled file links to the index.html new HtmlWebpackPlugin({ // The input file name template: 'src/index.prod.ejs', // Injects scripts into the <body> inject: true, // This is so hacky. I inject a string so the built .ejs file has this template var. Lets us set api_url when server is started instead of at bundle time. API_URL_TEMPLATE_VAR: '<%= process.env.API_URL %>', // The output file name filename: 'index.ejs' }), new ExtractTextPlugin("styles.css") ], 。我该怎么办?

2 个答案:

答案 0 :(得分:1)

  

但是我想要吐司中的代码值,而不是名称

然后,您需要实际解析JSON中的"code"而不是 "name",然后将其添加到适配器。

例如,解析它

String code = jsonObject.getString("code");
String name = jsonObject.getString("name");
items.add(code + " " + name);

祝酒吧

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        String[] parts = items.get(i).split(" ");
        Toast.makeText(getApplicationContext(),parts[0], Toast.LENGTH_LONG).show();
    }
});

答案 1 :(得分:-2)

试试这个。

json.getJSONObject("jsonData").getString("name");