【问题标题】:JSONObject don't Get data from QR codeJSONObject 不从二维码获取数据
【发布时间】:2026-01-29 20:35:01
【问题描述】:

当我扫描 QR 码时,它在 result.getContents 上获取数据,但没有通过 JSONObject obj 上的数据,只是直接继续 Catch

数据被传递到JSONObject obj = new JSONObject(result.getContents()); 这一行,但是当开始到Debug 时,它不会传递上一行中'obj' 上的数据。

这是我的代码:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            //if qrcode has nothing in it
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
                //if qr contains data
                try {
                    //converting the data to json
                    JSONObject obj = new JSONObject(result.getContents());
                    //setting values to textviews

                    if(obj.has("FN")) {
                        etFirstName.setText(obj.getString("FN"));
                    }
                    if(obj.has("N")) {
                        etLastName.setText(obj.getString("LN"));
                    }
                    if(obj.has("TITLE")) {
                        etTitle.setText(obj.getString("TITLE"));
                    }
                    if(obj.has("STATUS")) {
                        etStatus.setText(obj.getString("STATUS"));
                    }
                    if(obj.has("EMAIL")) {
                        etEmail.setText(obj.getString("EMAIL"));
                    }
                    if(obj.has("TEL;TYPE=work")) {
                        etPhoneHome.setText(obj.getString("TEL;TYPE=work"));
                    }
                    if(obj.has("TEL;TYPE=cell")) {
                        etPhonePrimary.setText(obj.getString("TEL;TYPE=cell"));
                    }
                    if(obj.has("ADR;TYPE=work")) {
                        etAddressLine1.setText(obj.getString("ADR;TYPE=work"));
                    }
                    if(obj.has("Street")) {
                        etAddressLine2.setText(obj.getString("Street"));
                    }
                    if(obj.has("Street")) {
                        etCity.setText(obj.getString("Street"));
                    }
                    if(obj.has("zip")) {
                        etZip.setText(obj.getString("zip"));
                    }


                } catch (JSONException e) {

                    Log.e(TAG, "notification= error" + e.toString());
                    e.printStackTrace();

                    /*etFirstName.setText(result.getContents());
                    etLastName.setText(result.getContents());
                    etTitle.setText(result.getContents());
                    etEmail.setText(result.getContents());*/
                    //if control comes here
                    //that means the encoded format not matches
                    //in this case you can display whatever data is available on the qrcode
                    //to a toast
                    Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

Logcat 见下文:

03-09 11:23:06.909 297-678/? E/SimpleSoftOMXComponent: 3
03-09 11:23:07.127 297-665/? E/audio_a2dp_hw: adev_set_parameters: ERROR: set param called even when stream out is null
03-09 11:23:12.899 26980-26980/com.example.crm E/AddContactActivity: notification= errororg.json.JSONException: Value BEGIN of type java.lang.String cannot be converted to JSONObject
03-09 11:23:13.040 297-8371/? E/SimpleSoftOMXComponent: 1
03-09 11:23:13.040 297-8371/? E/SimpleSoftOMXComponent: 2
03-09 11:23:13.041 297-8371/? E/SimpleSoftOMXComponent: 3

JSON 响应数据如下代码:

BEGIN:VCARD,
VERSION:2.1
FN:sss sss
N:sss;sss
TITLE:PHD
TEL;CELL:1111111111
TEL;WORK;VOICE:2222222222
TEL;HOME;VOICE:8888888888
EMAIL;HOME;INTERNET:abc@example.com
EMAIL;WORK;INTERNET:abc@example.com
URL:http://ABC@ABC.COM
ADR:;;sample address;ss;;102103;US
ORG:ss
END:VCARD

到处搜索,但我没有得到任何关于这个问题的结果,所以请帮助我:)

谢谢&问候 桑迪普·帕特尔

【问题讨论】:

  • 你能告诉我 logcat 错误吗
  • JSONObject obj = new JSONObject(result.getContents()); //
  • 见 logcat @PankajMundra
  • 首先在线验证您的 json 响应(例如:jsoneditoronline.org)。您的回复格式不正确。
  • 您的回复不是有效的 JSON,请参阅*.com/questions/10267910/…

标签: java android json


【解决方案1】:

您的数据不是 JSON,请查看输出中的 JSONLint reports 内容。 errororg.json.JSONException 通知您,在字符串的开头,Json 中不允许使用短语“BEGIN”。

{
    "json": "looks like this",
    "key": "value"
}

由于您的数据似乎是 vCard,因此您需要一个不同的解析库,例如https://github.com/mangstadt/ez-vcard

String str =
"BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"N:Doe;Jonathan;;Mr;\r\n" +
"FN:John Doe\r\n" +
"END:VCARD\r\n";

VCard vcard = Ezvcard.parse(str).first();
String fullName = vcard.getFormattedName().getValue();
String lastName = vcard.getStructuredName().getFamily();

所以在你的代码 sn-p 中:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        //if qrcode has nothing in it
        if (result.getContents() == null) {
            Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
        } else {
            //if qr contains data
            try {
                //converting the data to vCard object
                VCard vcard = Ezvcard.parse(result.getContents()).first();
                //setting values to textviews

                // "given name" is the first name
                etFirstName.setText(vcard.getStructuredName().getGiven());

                // "family name" is the last name
                etLastName.setText(vcard.getStructuredName().getFamily());
                ...

【讨论】:

  • 如果我想设置TITLESTATUS 那么如何设置??
【解决方案2】:

这是我为您提供的解决方案。 如果你得到像

这样的响应数据
        "
        BEGIN:VCARD,
        VERSION:2.1
        FN:sss sss
        N:sss;sss
        TITLE:PHD
        TEL;CELL:1111111111
        TEL;WORK;VOICE:2222222222
        TEL;HOME;VOICE:8888888888
        EMAIL;HOME;INTERNET:abc@example.com
        EMAIL;WORK;INTERNET:abc@example.com
        URL:http://ABC@ABC.COM
        ADR:;;sample address;ss;;102103;US
        ORG:ss
        END:VCARD
        " this than forgot the json object concept.

使用 vcard 库。你可以从这里获取 vcard 库 https://github.com/mangstadt/ez-vcard

通过使用此库,您可以使用以下代码获取您想要的任何联系数据,您可以在您的 editext. 中进行设置

String str =
    "BEGIN:VCARD\r\n" +
    "VERSION:4.0\r\n" +
    "N:Doe;Jonathan;;Mr;\r\n" +
    "FN:John Doe\r\n" +
    "END:VCARD\r\n";`

        VCard vcard = Ezvcard.parse(str).first();
        String fullName = vcard.getFormattedName().getValue();
        String lastName = vcard.getStructuredName().getFamily();

如果您对如何执行此操作有任何疑问。你可以再问我。

【讨论】:

  • 我想在你的例子中获取二维码数据而不是静态数据你打印静态数据所以可以获得动态数据
  • 它将适用于动态数据。如果 QR 码仍然提供多个联系数据,它将起作用。如果您的二维码生成 vcf 格式的数据,那么它肯定会起作用。当你扫描二维码时,它给出了什么信息?
  • 这个信息它给了我关于 catch block toast BEGIN:VCARD, VERSION:2.1 FN:sss sss N:sss;sss TITLE:PHD TEL;CELL:1111111111 TEL;WORK;VOICE:2222222222 TEL;HOME;VOICE:8888888888 EMAIL;HOME;INTERNET:abc@example.com EMAIL;WORK;INTERNET:abc@example.com URL:http://ABC@ABC.COM ADR:;;sample address;ss;;102103;US ORG:ss END:VCARD
  • 您在执行此行时正在捕获此信息。 JSONObject obj = new JSONObject(result.getContents());但我认为您可以通过 result.geContent() 获取数据。现在不需要解析json。在 result.getContent() 中获取数据后。您可以使用此行 VCard vcard = Ezvcard.parse(str).first(); String fullName = vcard.getFormattedName().getValue(); String lastName = vcard.getStructuredName().getFamily();你可以按照我的解决方案
  • 是的,在这一行我会得到数据,但它不会传递给obj 对象
【解决方案3】:

检查您的字符串数据。 确保您的字符串数据是 json 对象。

打印你的字符串并检查它是否是json。

【讨论】:

  • JSONObject 未创建,所以我被卡住了,所以你有任何解决方案
【解决方案4】:

内容不是 JSON,因此您无法将其转换为 JSONObject。 您可能需要 Vcard 的解析器。在 Github 中搜索一些库或编写自己的库。

【讨论】: