-3

このような文字列を取得しています。
type of を実行し、stringとして取得します。
文字列がかなり大きいので、その一部を掲載しています。

[System.SelectOption[value="account__c", label="Account", disabled="false"], System.SelectOption[value="account_owner__c", label="Account Owner", disabled="false"], System.SelectOption[value="accountname__c", label="Business Name", disabled="false"]]

私が必要としているのは、Json またはその配列です。助けてください。

4

2 に答える 2

2

これはあなたが探しているものですか?
このコードは基本的に、文字列をキーと値のペアの配列に変換します。

JSFiddleリンク:http ://jsfiddle.net/Dap6C/2/

var x = '[System.SelectOption[value="account__c", label="Account", disabled="false"], System.SelectOption[value="account_owner__c", label="Account Owner", disabled="false"], System.SelectOption[value="accountname__c", label="Business Name", disabled="false"]]';

x = x.substring(0, x.length - 1).substring(1, x.length).replace(/System.SelectOption/g,"");
var xp = x.substring(0, x.length - 1).substring(1, x.length).replace(/, /g,",");
var y = xp.split('],[');

var mainarray = [];
for(var i=0;i<y.length;i++){
 var z = y[i].split(',');
    var elements = [];
    for(var j=0;j<z.length;j++){
        var m = z[j] ;
        m =m.split('=');
        elements[m[0]] = m[1];
    }
    mainarray.push(elements);
}

var samplelement = mainarray[0];
document.write("<b>Sample output saved as key-value pairs = </b><br/>"+ "<b>value is </b> " + samplelement.value +  "<b>and label is </b> "+ samplelement.label + ' ');
于 2013-02-28T17:42:44.710 に答える