简体   繁体   English

解析Asynctask中的json,预计没有结果

[英]Parse json in Asynctask, No result expected

I'm trying to parse JSON in AsyncTask and so far everything works fine but does not show me the result I hope. 我正在尝试在AsyncTask中解析JSON,到目前为止一切正常,但没有显示我希望的结果。

PHP CODE: PHP代码:

  if($row>0){
$resultado["productos"][]=array("logstatus"=>$nombre);
 }
 else{
$resultado["productos"][]=array("logstatus"=>"0");
}
echo json_encode( $resultado );

RESULT: 结果:

{"productos":[{"logstatus":"nombre"}]}

JAVA CODE: JAVA代码:

    @Override
    protected Void doInBackground(Void... params) {
        // Create an array
                    arraylist = new ArrayList<HashMap<String, String>>();
                    // Retrieve JSON Objects from the given URL address
                    jsonobject = JSONfunctions
                            .getJSONfromURL("url/file.php");

                    if(jsonobject != null){         
                    try {
                        // Locate the array name in JSON
                        jsonarray = jsonobject.getJSONArray("productos");
                        for (int i = 0; i < jsonarray.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            jsonobject = jsonarray.getJSONObject(i);
                            // Retrive JSON Objects
                            map.put("logstatus", jsonobject.getString("logstatus"));
                            arraylist.add(map);
                        }

                    } catch (JSONException e) {
                        Log.e("Error", e.getMessage());
                        //e.printStackTrace();
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());         
                    }
                    }else{
                        //Log.e("Response","No data");
                    }
                    return null;
                }

    @Override
    protected void onPostExecute(Void args) {

        SymbolSet syms = scanner.getResults();
        for (Symbol sym : syms) {
            if("".equals(sym.getData())){
                Toast.makeText(getBaseContext(),"found", Toast.LENGTH_SHORT).show();    
                finish();
                barcodeScanned = true;
            }else{
                Toast.makeText(getBaseContext(),"Not found"+arraylist, Toast.LENGTH_SHORT).show();
            }
        }
        mProgressDialog.dismiss();          
    }

When you run the AsyncTask shows a Toast with the message: Not found = [{"logstatus": "name"}] and I need to remove [{}] and only get the value "name". 当您运行AsyncTask时,将显示一条带有消息的Toast:找不到= [{“ logstatus”:“名称”}],我需要删除[{}]并仅获取值“名称”。

How I can get that result? 我怎么能得到那个结果? Thanks to all 谢谢大家

如果arraylist中始终有一个对象,则可以使用该代码:

Toast.makeText(getBaseContext(),"Not found" + arraylist.get(0).get("logstatus"), Toast.LENGTH_SHORT).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM