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.

Community
  • 1
  • 1
Pedro Hoehl Carvalho
  • 2,183
  • 2
  • 19
  • 25
  • are you creating this array hard-coded like this `[28]` ? (like the code you shared) – Hacketo Sep 23 '15 at 14:27
  • 2
    Welll, I'm getting `"[28]"` on Firefox, which environment are you using? – MinusFour Sep 23 '15 at 14:29
  • Looks there is something with editor you wrote code. Like you have invisible special characters. – Stepan Tsymbal Sep 23 '15 at 14:36
  • Well, looks like you have this issue: http://stackoverflow.com/questions/3862430/differences-in-json-stringify-result-between-browsers – Sandeep Nayak Sep 23 '15 at 14:41
  • @Hacketo, the code is simplified. The array is is stored in a variable. – Pedro Hoehl Carvalho Sep 23 '15 at 14:44
  • 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. – MinusFour Sep 23 '15 at 15:09
  • Does your simplified example produce the same problem when you run it? If not then you need to prude an example that does demonstrate the problem. – bhspencer Sep 23 '15 at 17:19
  • HI @bhspencer, Thank you for commenting on your downvote. Since MinusFour's comment, I've updated the question to reflect the actual problem. It seems to be a problem with the strings I'm getting from the server (which I couldn't possibly demonstrate here), but I'll try to update the question as I get new information. – Pedro Hoehl Carvalho Sep 23 '15 at 17:29

2 Answers2

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.

Community
  • 1
  • 1
Pedro Hoehl Carvalho
  • 2,183
  • 2
  • 19
  • 25
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 ......

narainsagar
  • 1,079
  • 2
  • 13
  • 29
  • 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) – Pedro Hoehl Carvalho Sep 23 '15 at 20:08