Convert string to JSON

Asked

Viewed 1,669 times

-2

I got the following object:

1,2,3,4,5,6

I would like to convert it to JSON to recover in php to perform a for loop:

{"id":"1"}
{"id":"2"}
{"id":"3"}
{"id":"4"}
{"id":"5"}
{"id":"6"}

I take this amount from bootstrap-multiselect.

Following example:

<select id="category" class="form-control" multiple="multiple">
    <option value="1">Aventura</option>
    <option value="2">Comedia</option>
    <option value="3">Ficção</option>
    <option value="4">Drama</option>
    <option value="5">Novela</option>
    <option value="6">Category</option>
</select>

Javascript:

<script>    
    $('#category').click(function() {    
        alert($('#category').val());    
    })
</script>

Example: http://jsfiddle.net/pmrotule/w7aakdbb/54/

  • 1

    the JSON you gave as an example is invalid, what kind of structure you need to receive on the server?

  • A JSON object cannot have repeated keys, not to know if you want to create an array of ids, an array of objects.

  • altered. please check if it is correct

2 answers

1


According to the example you indicated on Jsfiddle, the value you get from select is already a array, therefore the solution of Aline should work:

// Valor que vem do multi-select:
var values = ["1", "2", "3", "4", "5"];

// Cria uma lista de objetos:
var result = [];

values.forEach(function(value) {
  result.push({id: value});
}); 

// Converte o objeto para JSON:
var json = JSON.stringify(result);

// Exibe o resultado em JSON:
console.log(json);

Your result will be a list of objects:

[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]

An alternative is to create only one object and set the list of id as its value:

// Valor que vem do multi-select:
var values = ["1", "2", "3", "4", "5"];

// Cria um objeto de índice "id" e valores "value":
var result = {id: values};

// Converte o objeto para JSON:
var json = JSON.stringify(result);

// Exibe o resultado em JSON:
console.log(json);

The result, in this form, will be:

{"id":["1","2","3","4","5"]}

Which in my opinion would make more sense, but depends on its application.

  • is not working the first way.. In this second way, you can make a for loop in php? to take these values one by one?

  • Yes, it does. Like you tried to do?

  • I’m still having to display on the console.log, but I realized that my value is coming as object and not as string

  • This information didn’t help much. What do you want to do, what did you do and why didn’t it work? What was the mistake?

  • It works when I send a string directly to convert to vector, but I realized that the value I’m receiving is an object, so the split no longer works, but I’m trying to inform you

  • But you had informed that it was a string: https://answall.com/questions/204125/converter-string-em-json/204160?noredirect=1#comment418157_204150

  • this, in my view I thought it was a string, but I gave the command typeof value and it informed me that it is an object, I really missed

  • Then check there how it is in fact and edit your question to be the correct way. After that I edit the answer according to the question.

  • Ready! I edited. But I managed to get the result the second way you suggested. Thank you very much! Only that went out: Object {id: "["2","3"]"} has some problem?

  • 1

    For the example you put in Jsfiddle, you already receive the value as a array, then Aline’s solution should work. You must have done something wrong.

  • Poxa was just what you said. It came as an array, but I thought the only thing I saw there was a string. My mistake. Your two options solve.. I’m sorry. And thank you very much once again

Show 6 more comments

0

Assuming you have an array of integers then:

var lValoresDoSelect = [1,2,3,4,5];

var lIds = new Array();
$.each(lValoresDoSelect, function(index, obj){
   lIds.push({ id: obj });
});

Assuming you will get this information from an element:

var lIds = new Array();
    $.each($(".multiselect:selected"), function(index, obj){
       lIds.push({ id: $(obj).text() });
    });
  • Hi Aline, two things it might be interesting to add to your answer: (1) Explain why you have created an arrangement of objects rather than just an object (in this case, why the key id repeats and would not be possible in only one, so the arrangement) and (2) comment how is made the conversion of a Javascript arrangement to JSON, using the function JSON.stringify, since this is the main purpose of the question.

  • value does not come in vector, it comes in string

Browser other questions tagged

You are not signed in. Login or sign up in order to post.