How to transform data into array with JSON?

0

I have to get the data being accumulated in the valore variable and turn them into an array so that I can get them using HttpRequest in an HTML page. How do I do this?

// Classe para chamar o JSON.
function json(){
	var qtd;
	var retorno;

	// Resgatar valores.
	json.prototype.resgatarValores = function(){
		$('#resultado').html('Carregando dados...');

		// Estrutura de resultado.
		$.getJSON('/webpro/webadm/lncidjson', function(data){
			this.qtd = data.usuarios.length - 1;
			this.valore = '';
			this.label = '';

			for (i = 0; i < this.qtd; i++){
				if(i == (this.qtd - 1)) {
					this.valore += data.usuarios[i].valor;
					this.label += data.usuarios[i].descr;
				} 
				else {
					this.valore += data.usuarios[i].valor + ',';
					this.label += data.usuarios[i].descr + ',';
				}
			}

			$('#valor').html(this.valore);
			$('#label').html(this.label);
		});

	}

}

// Objeto.
var obj = new json();
obj.resgatarValores();
    
asked by anonymous 16.03.2015 / 15:40

3 answers

3

//obj.resgatarValores();
var obj= [{nome:"Marcelo",idade:20},{nome:"Teste",idade:90}];

var b =JSON.stringify(obj);
document.writeln(b)
    
23.03.2015 / 14:10
0

If you are using JQUERY ... Your answer is in the documentation:

link

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );
    
17.03.2015 / 13:01
0

You can do this conversion in the web layer, as presented by user3120449, how to do in the backend depending on the language used, in .NET we have the JavaScriptSerializer class that performs the work.

    var keyValues = new Dictionary<string, string>
           {
               { "emailSend", textBox1.Text },
               { "toEmail", textBox2.Text }
           };

    JavaScriptSerializer js = new JavaScriptSerializer();
    string json = js.Serialize(keyValues);
    MessageBox.Show(json);
    
14.06.2016 / 15:22