Francisco Lawrence
Francisco Lawrence

Reputation: 11

Parson JSON Object from QR Code after being scanned

In my qr code i have this JSON Object :

JSONObject json = new JSONObject();
    try {
        json.put("name", editTextName.getText().toString());
        json.put("price", editTextPrice.getText().toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    BarcodeQRCode barcodeQRCode = new BarcodeQRCode(json.toString(), 1000, 1000, null);
    Image codeQrImage = barcodeQRCode.getImage();
    codeQrImage.scaleAbsolute(100, 80);
    document.add(codeQrImage);

But when i try to parse it,

//this is the qr code context after scanned, i just declared it for clarifications
  String scannedQrCode = "\"name\":\"Nissan Urvan Original 12v\",\"price\":\"2500\"}";
 try {
                    mainObject = new JSONObject(scannedQrCode);
                    String nameItem = (String) mainObject.get( "name" );
                    String priceItem = (String) mainObject.get( "price" );
                    Log.e("TESTING",""+nameItem +priceItem);
                } catch (JSONException jsonException) {
                    jsonException.printStackTrace();
                }

it says in the logcat that :

org.json.JSONException: Value name of type java.lang.String cannot be converted to JSONObject

is there a problem with my json object? is it on the creation?

Upvotes: 0

Views: 736

Answers (1)

CoolMind
CoolMind

Reputation: 28875

Add starting { in "\"name\":\"Nissan Urvan Original 12v\",\"price\":\"2500\"}":

"{\"name\":\"Nissan Urvan Original 12v\",\"price\":\"2500\"}"

Upvotes: 1

Related Questions