lightswitch05 / table-to-json

Serializes HTML tables into JSON objects.
http://lightswitch05.github.io/table-to-json/
MIT License
756 stars 172 forks source link

How to return JSON array instead of text for specific column #29

Closed SamehMahmoud closed 7 years ago

SamehMahmoud commented 8 years ago

I want to return a JSON array from an extractor like this:

`

10 : function(cellIndex , $cell){
                    var prlArray = [];
                    $cell.find('select option').each(function(){
                        //alert(this.value);
                        prlArray.push(this.value);
                    });
                    return JSON.parse(prlArray.toString());
                }

`

This extractor is causing error in because of "return JSON.parse(prlArray.toString());" thanks in advance

lightswitch05 commented 8 years ago

does this not work?

function(cellIndex , $cell){
    var prlArray = [];
    $cell.find('select option').each(function(){
        //alert(this.value);
        prlArray.push(this.value);
    });
    return prlArray;
}
SamehMahmoud commented 8 years ago

It does, but it is concatenated to the rest data as String not array like this : { parallelActions: "8,2,3"} but what i need is to be like this: {parallelActions : [8,2,3]}

lightswitch05 commented 8 years ago

Just looked at the code. It will always convert whatever you give it into a string. As far as I know there is no way around this in the current version.

Mottie commented 8 years ago

You could process this.value before pushing it into the prlArray.

Maybe you can try using this simple jQuery plugin called nVal()... use $(this).nVal() and it will return an array of numbers.

lightswitch05 commented 8 years ago

@Mottie I think the problem is that anything returned from the extractor function is automatically converted to a string by the $.trim function: https://github.com/lightswitch05/table-to-json/blob/master/src/jquery.tabletojson.js#L55

koshuang commented 7 years ago

@SamehMahmoud I @lightswitch05 I submitted a PR #37 which should be able to fix this issue.

lightswitch05 commented 7 years ago

released in 0.12.0