Huliganul
Huliganul

Reputation: 71

Json number format

I have the following code :

JSONArray jsonArray = new JSONArray(new String(cod_bare));
    for (int i=0;i<jsonArray.length();  i++) 
        {
            cod_in_loop=jsonArray.getString(i);
            .....
        }

code_bare = [085845,0547561....]

The problem is that cod_in_loop show the number without de 0 => 085845 it ends in cod_in_loop just 85845. I need the full number "085845"

What to do ?

Upvotes: 0

Views: 3474

Answers (2)

Kevin F
Kevin F

Reputation: 169

 I need the full number "085845"

I can't understand why you are using Json. If code_bar is a String I think that you must split the string

//java code
String code_bar = "[1,2,3,4,5,6"];
        String[] arrayNumbers = code_bar .split(",");
        for (int i = 0; i < arrayNumbers .length; i++) {
            System.out.println(arrayNumbers [i]);
        }

**You must add something for delete "[" and "]"

Finally if you need use json you must add { and } in code_bar:

//c# code:
var array = JArray.Parse(code_bar.Trim('{', '}'));
List<String> listNumbers = array.ToObject<List<String>>();

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074238

The problem is the JSON, not the code. If your JSON is really

[085845,0547561....]

then it's invalid; in JSON, you're not allowed to start a number with a 0 unless the 0 is immediately followed by a . (e.g., 0.1), as we can see from the handy diagram from http://json.org:

enter image description here

...and from this text in the RFC:

The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed.

(my emphasis)

So the fix here is to correct the JSON. If you want to preserve the leading zero, then that pretty much means that the thing you're dealing with isn't a number, it's a series of digits — e.g., a string, so:

["085845","0547561"....]

Upvotes: 7

Related Questions