0

I'm trying to send a string array as a parameter on a get request.

console.log(arrayOfStrings); //Prints ["28"]
var ids = JSON.stringify(arrayOfStrings);
console.log(ids); //Prints ["\u00002\u00008"]   

$http.get('do_staff_search', { params:{'ids': ids } }).then(successHandler);

However, when I stringify the number array, I get ["\u00002\u00008"] which then causes an error on the server java.lang.NumberFormatException: For input string: "▯2▯8" with the two rectangular blocks in front of each number.

If I use Google Chrome's console, create the same array and stringify it, the output is "["28"]"

This seems like a trivial issue, but I couldn't find a good similar question on Stack Overflow.

UPDATE

I did some tests and it turns out @MinusFour is correct. It is an array of strings, not an array of integers as I assumed (the array is the payload from another request).

UPDATE 2

I tried converting the string array to an integer array using this function:

function arrayOfNums(arr){
    var  newArr = [];
    for (var i = 0; i < arr.length; i++) {
        newArr[i] = parseInt(arr[i]);
    };
    return newArr;
}

But parse Int is returning NaN for each element. Now i'm wondering if there is some encoding issue with my strings that cold be causing it, since I got them from a server request I made earlier. I found this related question but I'm not sure how I would escape any invalid characters.

Just as some background, the array is stored as a CLOB on an SQL DB. I'm using Spring and Jackson on the server side to send a JSON object back, and within this object I have the array in question. Although I have access to the code on the server, I can't really change it because there are other applications that make requests to it.

8
  • are you creating this array hard-coded like this [28] ? (like the code you shared) Commented Sep 23, 2015 at 14:27
  • 2
    Welll, I'm getting "[28]" on Firefox, which environment are you using? Commented Sep 23, 2015 at 14:29
  • Looks there is something with editor you wrote code. Like you have invisible special characters. Commented Sep 23, 2015 at 14:36
  • Well, looks like you have this issue: stackoverflow.com/questions/3862430/… Commented Sep 23, 2015 at 14:41
  • 2
    @PedroHoehlCarvalho, well since this isn't the real code we can't find the real problem. My best guess would be that the element of the array is not a number, but a string. Again, nobody will be able to tell, because we don't have the real code. Commented Sep 23, 2015 at 15:09

2 Answers 2

0

It seems the strings are coming with some invalid characters back from the AJAX request as described here

So before running the array through JSON.stringify, I'm cleaning each string up like this:

function arrayOfNums(arr){
    var numberPattern = /\d+/g;
    var  newArr = [];
    for (var i = 0; i < arr.length; i++) {
        newArr[i] = parseInt(arr[i].match( numberPattern ).join(""));
    };
    return newArr;
}

Because there are invalid characters in front of each digit, I use join to concatenate all digits together after matching the pattern.

More of a work around than a permanent solution, I just hope this helps someone in a similar situation.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, i think your question is bit confusing!

It might be giving because you are applying JSON.stringify() on string array.. that's what i am seeing in your code.. (please check!)

make change: try to change it to an integer array

for example this:

arrayOfStrings = [28];

and then check!

Below is just an other example i tested:

var test_arr = [1, 2, 3, 5]; // array of integers
console.log(test_arr); // will print: [1, 2, 3, 5] -- an integer array

var test_ids = JSON.stringify(test_arr);
console.log(test_ids); // // will print: "[1,2,3,5]" -- an string

var test_parse = JSON.parse(test_ids);
console.log(test_parse ); // will print: [1, 2, 3, 5] -- an integer array

just a guide on JSON.parse() vs JSON.stringify() link: https://msdn.microsoft.com/en-us/library/cc836459(v=vs.94).aspx ......

1 Comment

I ended up doing precisely that (See update 2). But I was getting NaN when trying to parse the strings in the array, so I had to use RegEx to extract the digits from the strings (see my answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.