- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我扫描 QR 码时,它在 result.getContents
上获取数据,但没有传递 JSONObject obj
上的数据,只是指示继续 Catch
block
数据传递到这一行 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
到处搜索,但我没有得到关于这个问题的任何结果,所以 plzzz 帮助我:)
谢谢&问候桑迪普帕特尔
最佳答案
你的数据不是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();
所以在你的代码 fragment 中:
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());
...
关于java - JSONObject 不从二维码获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49187345/
我找不到答案。尝试了很多网站,包括这个。我想知道问题出在哪里。 错误是:org.json.JSONException:java.lang.String类型的值连接无法转换为JSONObject 这是我
我从 Java 应用程序提交了一个查询,该查询在 Elasticsearch 服务器上运行时以字符串形式返回结果。我希望结果为 JSONObject 列表对象。我可以将字符串转换为 JSONObjec
我正在尝试解析 JSON 字符串,但在尝试获取嵌套对象时出现错误: JSONObject jsonObject = new JSONObject(jsonString); System.out.pri
我有这样的 API 输出: {"user" : {"status" : {"stat1" : "54", "stats2" : "87"}}} 我从这个 API 创建了一个简单的 JSONObject
我正在寻找一种方法来比较两个 JSONObjects 除了使用 JSONObject.toString().equals(JSONObject.toString()) 并遇到了这种类似的方法,但它的比
我在读取和写入 Json 文件时遇到问题。我想将一些内容附加到 json 文件中,但它无法正常工作:它只是放入一个新的 json 对象,而没有使用 ',' 将其与前一个对象分开。我在每个网站上到处搜索
我有 JSON 文件:“D://test.json”,它遵循 MongoDB 格式: { "key":"value" } { "embedded": { "key2": "value2" } } {
我想要一个像这样的 json 文件: { "resu0": {"item":"value"} "resu1": {"item":"value"} "resu2": {"item":"value"} }
我有一个像这样的 JSON { "result": { "issue_date": "xx-yy-zzzz", "father/husband": "TEST",
我想发送json,里面有另一个json对象,像这样 { "key1": "value1", "key2": "valu2", "content": {
private JSONObject insertJSONintoJSON(JSONObject newJSON, JSONObject parent) { Object[] a = pare
常规更新:我之前询问是否可以按原样获取 JSONObject 的值,以便稍后将其与覆盖的版本进行比较。我的第一次尝试是: JSONObject oldObj = new JSONObject(); p
我正在尝试创建一个具有特定键和空值的 JSONObject。但该值应该是 JSONObject 而不是 JSONArray。 我已经使用 localJson.append(key, JSONObjec
我得到的所有资源都是从 JSONArray 获取 JSONObject。但在这种情况下,我想获得“temp”的温度。我不认为它是数组的一部分。我怎样才能得到它? { "coord":{
我正在尝试解析 JSONObject 以获取 JSON 数组的数据。 但问题是 JSONParser 是 org.json.simple.JSONParser 中的一个类,而 JSONObject 位
我发现 variable instanceof JSONObject 有奇怪的行为。我有以下代码: Object value; try { value = customer.get(key);
JsonObject 和 JSONObject 有什么区别? 当我们使用 put、add 和 addproperty 方法时,我有点混淆 JsonObject 和 JSONObject。 提前致谢。
我想从 Another JSONObject 中的一个 JSONObject 中检索数据谁能告诉我如何取回它们 我的JSONObject是这样的 {"udetail":{"ID":238597,"Re
你是否注意到,如果你使用 put() 将一个 JSONObject 放入另一个 JSONObject,容器 JSONObject 将不会存储对第一个 JSONObject 的引用,而是一个新的 JSO
我的应用程序构建得很好,但是当我运行它时,当以下行尝试执行时,我不断收到异常: JSONObject json = new JSONObject(); 我收到以下异常: 异常 javax.servle
我是一名优秀的程序员,十分优秀!