j.j.a.c.k.
j.j.a.c.k.

Reputation: 465

Android JSONObject.getString() scrambles large numbers

When I try to send JSONObject from server I have number 00420123456789 like phone number (but saved in DB and sent from the server as string!!)

When I get it in android it is in JSONObject shortened to 420123456789 it is no problem for me I can add two more zeros at the beginning

BUT

when I try String s = JSONObject.getString("myNumber") it returns 4.20607238118E11.

Can somebody tell me anything to this?? I don't know what to do tried a lot of google searching but it looks like no one is sending long numbers in JSON.

EDIT: shortened and changed personal data but this is JSON I am receiving:

{
        Id: 1,
        name: myName,
        city: Prague,
        street: Karlovarska,
        street_number: 48,
        postal_code: 50280,
        phone_number: 420123456789,
        email: [email protected],
        webpage: www.aaa.com/page.aspx,
    }

another problem appeared: in JAVA(android) using

JSONObject json = new JSONObject(jsonStringFromWeb);

everything works until I send webpage with "/" I tried escaping by // or / but nothing works...

Upvotes: 1

Views: 181

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007296

Change the JSON to make phone_number be a string, not an integer:

{ Id: 1, name: myName, city: Prague, street: Karlovarska, street_number: 48, postal_code: 50280, phone_number: "420123456789", email: [email protected], webpage: www.aaa.com/page.aspx, }

After all, not all phone numbers in the world are integers.

Similarly, street_number and postal_code should be strings, as not all street numbers (221B) or postal codes (most of them in the UK) are integers.

Upvotes: 2

amiko
amiko

Reputation: 109

When you put into the long type, it may lead to this situation, you can pass in the incoming String type. like this:

JSONObject json=new JSONObject();
json.put("myNumber",420123456789.toString())

Upvotes: 0

Related Questions