Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'arrays in arrays' #13

Closed
GoogleCodeExporter opened this issue Nov 9, 2015 · 7 comments
Closed

'arrays in arrays' #13

GoogleCodeExporter opened this issue Nov 9, 2015 · 7 comments

Comments

@GoogleCodeExporter
Copy link

Looking for a way to avoid the "old" <element>_asArray and still have the 
opportunity to have nested multiple elements (arrays in arrays)

Example:
-------------
var x2js = new X2JS();
var json = x2js.xml2json(xmldoc);
x2js.asArray(json.setup.icons.icon);
x2js.asArray(json.setup.templates.template); //asArray OK
x2js.asArray(json.setup.templates.template.states.state); //DOOOOhhh!!
-------------
Uncaught TypeError: Cannot read property 'state' of undefined 
-------------

It makes sense. Fundamental stufff :) I need to loop all states and call 
asArray on each template.

But it would be real nice if there was a way anyway.. Maybe a naming 
convention... something like:

var x2js = new X2JS();
x2js.becomesArray("json.setup.icons.icon");
x2js.becomesArray("json.setup.templates.template"); 
x2js.becomesArray("json.setup.templates.template.states.state"); 
var json = x2js.xml2json(xmldoc); 


Original issue reported on code.google.com by jmj.bak...@gmail.com on 11 Oct 2013 at 9:11

@GoogleCodeExporter
Copy link
Author

Hmm... It is interesting idea. Thanks! I'll think about it.

Original comment by abdulla....@gmail.com on 11 Oct 2013 at 9:32

  • Changed state: Accepted
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

And Thank you! I forgot :) 
Love this lib. 
Will just stroll down the "old" _asArray-road for now. NP!

Inspiration could by the way be found here (in actionscript):
http://krasimirtsonev.com/blog/article/as3-action-script-convert-xml-to-json-xml
2json 

Original comment by jmj.bak...@gmail.com on 11 Oct 2013 at 9:48

@GoogleCodeExporter
Copy link
Author

I've just added a new feature to v1.1.3
(You always can look it at 
https://code.google.com/p/x2js/source/browse/xml2json.js or download from 
https://x2js.googlecode.com/hg/xml2json.js)

You can do now like this, for your example:
var x2js = new X2JS({
   arrayAccessFormPaths : ["json.setup.templates.template", "json.setup.templates.template.states.state"]
});
var json = x2js.xml2json(xmldoc);

And access like the other JS objs:
json.setup.templates.template[0].states.state

You can specify also paths to arrays as RegEx form:
var x2js = new X2JS({
   arrayAccessFormPaths : [/.*\.template/, /.*\.state/]
});

Let me know if it works for you! Thanks!

Original comment by abdulla....@gmail.com on 12 Oct 2013 at 11:11

@GoogleCodeExporter
Copy link
Author

Also if you need some powerful XPath like tool on JSon there some additional 
libraries. 
For example I've just googled it - http://goessner.net/articles/JsonPath/ :)

Original comment by abdulla....@gmail.com on 12 Oct 2013 at 11:36

  • Changed state: Fixed

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

Perfect!  Nothing less.
Rewrote my project to use the arrayAccessFormPath  and it simply worked. 
Thank you!

Haven't tested the json xpath yet. But I sure will. 

Original comment by jmj.bak...@gmail.com on 14 Oct 2013 at 8:32

@GoogleCodeExporter
Copy link
Author

I appreciate for your feedback and interest! Thank you!
Well, I'm closing this one for now then.

Original comment by abdulla....@gmail.com on 14 Oct 2013 at 8:38

  • Changed state: Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant