-2

因此,我从 JSON 对象中获取了一堆字符串值,然后将它们全部打印出来。但是,我想将所有值放在一个字符串数组中并检查它们是否为空,如果是...将值更改为“N/A”,这样它们就不仅仅是空白了。但是,它可以识别哪些字符串是空白的,但是当我在 logcat 中检查它们时,它不会打印空字符串的名称。例如,我知道两个字符串将是空的,但是当我用这个检查它时:

System.out.println(nullCheck[i] + "is empty");

“是空的”在 logcat 中只出现了两次。即使我想更改其中任何一个的值,它仍然不会这样做。它检索所有包含数据的字符串并将它们打印出来。我可能只是忽略了一些非常简单的事情,但我会很感激任何帮助。以下是供参考的完整片段:

    String supplieraddress = "";
    String supplierphone = "";
    String supplieremail = "";
    String supplierfax = "";
    String vouchercontact = "";
    String supplierid = "";
    String suppliername = "";
    String servicetype = "";
    String serviceid = "";
    String vouchernotes = "";

    try {
        voucher = reservation.getJSONArray("vouchers").getJSONObject(vouchNumber);

        supplieraddress = voucher.getString("supplieraddress");
        supplierphone = voucher.getString("supplierphone");
        supplieremail = voucher.getString("supplieremail");
        supplierfax = voucher.getString("supplierfax");
        vouchercontact = voucher.getString("vouchercontact");
        supplierid = voucher.getString("supplierid");
        suppliername = voucher.getString("suppliername");
        servicetype = voucher.getString("servicetype");
        serviceid = voucher.getString("serviceid");
        vouchernotes = voucher.getString("vouchernotes");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String[] nullCheck = {supplieraddress, supplierphone, supplieremail, supplierfax, vouchercontact,
            supplierid, suppliername, servicetype, serviceid, vouchernotes}; 

    for(int i = 0; i < nullCheck.length; i++)
    {
        if(nullCheck[i].equals(""))
        {
            System.out.println(nullCheck[i] + "is empty");
            nullCheck[i] = "N/A";
        }

    }

    content.setText(Html.fromHtml(
            "</br><b>Supplier Address </b><br/>" + supplieraddress
            + "<br/><br/><b>Supplier Phone </b><br/>" + supplierphone
            + "<br/><br/><b>Supplier Email </b><br/>" + supplieremail
            + "<br/><br/><b>Supplier Fax </b><br/>" + supplierfax
            + "<br/><br/><b>Voucher Contact </b><br/>" + vouchercontact
            + "<br/><br/><b>Supplier ID </b><br/>" + supplierid
            + "<br/><br/><b>Supplier Name </b><br/>" + suppliername
            + "<br/><br/><b>Service Type </b><br/>" + servicetype
            + "<br/><br/><b>Service ID </b><br/>" + serviceid
            + "<br/><br/><b>Voucher Notes </b><br/>" + vouchernotes
            ));  

}

多谢你们!

4

1 回答 1

1

在你的try块中这样做:

String s = voucher.getString("supplieraddress");
supplieraddress = TextUtils.isEmpty(s)? "N/A" : s;
s = voucher.getString("supplierphone");
supplierphone = TextUtils.isEmpty(s)? "N/A" : s;
s = voucher.getString("supplieremail");
supplieremail = TextUtils.isEmpty(s)? "N/A" : s;

// and so on....
于 2012-04-06T19:42:23.017 回答