1

My function gets a string from a webservice, which represents a multi layered array structure. I then assign it to a variable. Something like this:

//incoming = [ [1,2,3], ['a','b','c'], [{v:1437, f:'1437 - Point A'}, {v:1440, f:'1440 -  Point B'}, {v:1445, f:'1445 -  Point C'}], [6.7,8.2,9.2] ]  -- this is the incoming string
   arrays =  incoming;

When I try to pass this to another function, javascript always warns me that this is a string, not an array (which the function requires to split it up later to get the smaller arrays).

So far I tried this:

var globalArray = [];

$.each(arrays, function(key, object) {
    var innerArray = [];
    innerArray=object;
    globalArray.push(innerArray);
});

But then the globalArray will not contain multiple arrays, but a lot of strings which will become siblings.

[ 1,2,3,a,b,c,{v:1437, f:'1437 - Point A'}, {v:1440, f:'1440 -  Point B'}, {v:1445, f:'1445 -  Point C'},6.7,8.2,9.2 ] 

How could I recreate the array structure based on the content of the string?

9
  • please add the string, you get. Commented Jan 8, 2017 at 17:16
  • 1
    what is "a" in the array string? Commented Jan 8, 2017 at 17:19
  • 2
    why not change the input at the webservice to a valid JSON string? Commented Jan 8, 2017 at 17:21
  • Can you please show me how it would look like? Whenever I used JSON, I had names before the actual values as keys, so I could decompress. In a case like this I am bit unfamiliar of it's technique, how can I decompress it to an array which has the inner arrays in it. Commented Jan 8, 2017 at 17:22
  • assuming a right formated array, a JSON string looks like '[[1,2,3],["a","b","c"],["e","f","g"],[6.7,8.2,9.2]]' for it. Commented Jan 8, 2017 at 17:24

2 Answers 2

1

You could use a JSON string

[[1,2,3],["a","b","c"],[{"v":1437,"f":"1437 - Point A"},{"v":1440,"f":"1440 - Point B"},{"v":1445,"f":"1445 - Point C"}],[6.7,8.2,9.2]]

as defined by the standard and parse it with JSON.parse for getting an object.

var json = '[[1,2,3],["a","b","c"],[{"v":1437,"f":"1437 - Point A"},{"v":1440,"f":"1440 - Point B"},{"v":1445,"f":"1445 - Point C"}],[6.7,8.2,9.2]]',
    object = JSON.parse(json);

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Thank you Nina for the detailed answer and example!
1

Step 1 - Output valid JSON from your endpoint, something like this:

[ 
  [1,2,3], 
  ["a","b","c"],
  [
    {"v":1437, "f":"1437 - Point A"}, 
    {"v":1440, "f":"1440 -  Point B"}, 
    {"v":1445, "f":"1445 -  Point C"}
  ],
  [6.7,8.2,9.2] 
]

Step 2 - Parse the input with JSON.parse()

Not sure what's going on with the service, but literally every language has multiple decent JSON encoder/parser. Please use them. Never try to manually serialize your data especially if it's almost json.

1 Comment

Thank you for the answer! As suggested, I made sure to use a JSON instead of a String. I wish I could accept two answers at the same time. Thank you again!

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.