How to parse string array in Ajax? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a javascript in the html to render the html on client. I know how to do it using JSON. My question is: Is it possible without JSON?
For example, my server will reply array such as [["good", "bad"],["first", "second", "third"]] using a servlet. I mention String array in topic, because the xmlhttp.responseText is regarded as a text or string. So in javascript, how do I convert this result into array variable?
Using JSON, my server has to reply
{
"1": ["good", "bad"],
"2": [....]
}
I just wanted to see whether we can avoid this key string.

As already pointed out in the comments, JSON has quote:
two primary data structures: ordered lists (recognized as 'arrays')
and name/value pairs (recognized as 'objects')
JSON objects are written inside curly brackets and can contain multiple key/value pairs.
JSON arrays are written inside square brackets (elements of the array can be basic types like number or string but also objects or arrays).
(from http://www.w3schools.com/json/json_syntax.asp)
So [["good", "bad"],["first", "second", "third"]] is in JSON format and doesn't need to be converted to an object.

Related

How can I parse a stringified array of arrays (JavaScript)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have a string which represents an array. The items in the array can be characters, or arrays. So a string might look like: [a,b,[[],[d]],[],[[[]]],[[c,[t,s]],b]]
And I want to parse that out so it's a proper array with subarrays etc
So "[a,b,[c,[d,e],[]]]" would become:
[ 'a',
'b',
[ 'c',
['d','e'],
[]
]
]
Is there an easy way to do this in JavaScript? Eg some kind of array equivalent of JSON.parse? I tried JSON.parse but it throws an error (not unexpectedly).
You would have to process it to convert it into a format that JSON.parse would be able to handle. Your test case is simple so it is possible with an easy regular expression.
const str = "[a,b,[c,[d,e],[]]]";
const obj = JSON.parse(str.replace(/([a-z]+)/gi,'"$1"'));
console.log(obj);

How to get data in object without looping [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get data from the object without for loop
this is my object
data = [{name:"A",value:[java:45,c++:50]},{name:"B",value:[java:12,c++:47]},{name:"C",value:[java:15,c++:32]}]
Expected result:
result_name = ["A","B","C"]
result_java = [45,12,15]
The structure of your array data is invalid. You wrote nested array value in square brackets yet the items are written as key-value pairs.
There is no magical way you can obtain multiple data from an array without iteration/looping. (unless you already know the indices, but this defeats the purpose of using arrays)
That said, if you meant to avoid only the "for" loops, you can:
Fix your data as below.
let data = [
{name:"A", value:{java:45,"c++":50}},
{name:"B", value:{java:12,"c++":47}},
{name:"C", value:{java:15,"c++":32}}
];
Run an alternative loop such as below.
console.log(data.map(item => item.name)); // ["A", "B", "C"]
console.log(data.map(item => item.value.java)); // [45,12,15]

Is there a way that this is possible? I am trying to find a value of a var constructed by other variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a little bit of my code. I took out the complex parts. It is all on the script tag.
var trueenemyscoutx = '_2'
var tryeenemyscouty = '2'
var type_22 = "none"
var move1;
enemymove = "type" + trueenemyscoutx + trueenemyscouty;
var move1 = (enemymove.valueOf()).valueOf()
In the post, enemymove is a string constructed by concatenating "type" and two other variables, each of which is also a string.
The resultant string contains no information about the variables used and so the general answer to the question is no, it is not possible using the approach taken.
Obvious alternatives include:
Keep move a string, but use a separator (e.g. colon, comma or space) between component substrings. This allows extracting an array of the components using move.split(separator).
Use an array or simple object to hold move information instead of a string in the first place.

Getting json-keys after user uploads the json file without submitting the page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an HTML page with a form to upload JSON files. I want to extract the keys from the JSON file without submitting the page. Later in the same page, I want to use these keys as option fields in certain form inputs.
So I have JSON Array in the file which contains multiple JSON Objets. I want to get all the keys from that JSON object. {"foo": "bar", "baz": "baq"} where "foo" and "baz" are the keys. These keys I want to use further in my form.
Can anyone help me out here?
What do you understand with "keys"?
Assuming your JSON is: {"foo": "bar", "baz": "baq"}
If you understand "foo" and "baz" as keys, you need to use filereader api (https://developer.mozilla.org/en-US/docs/Web/API/FileReader) to read the file contents, then use JSON.parse() method to convert the file to json object and then iterate the object to get the keys:
var keys = [];
for (var x in obj){
if (obj.hasOwnProperty(x)) keys.push(x);
}
then use the keys array to generate whatever you need to.
Janis

I want to seperate the PHP JSON string in jQuery [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem with separating the json array result. When I alert the json result in jquery it returns me the following array.
{"productid":"17","product_quantity":"2"}{"productid":"9","product_quantity":"1"}
Now I want to separate every value in a different variable.
Thanx in advance.
that is not a valid json string.
first, you can convert the input to json string.
then, you can use JSON.parse to get the js array.
e.g.
first you need to do this:
input = '[{"productid":"17","product_quantity":"2"},{"productid":"9","product_quantity":"1"}]'
then:
input_array = JSON.parse(input)
It might be that your server returns a string that is not valid JSON. A valid example would be:
[{"productid":"17","product_quantity":"2"},{"productid":"9","product_quantity":"1"}]
How are you creating the json? Since you tagged PHP, the correct way (if you have an array) is like this, and it will return valid JSON, that you JS can handle:
echo json_encode($array);
The json you gave is misformed. I assume that's a typo.
Use JSON.parse to convert to a javascript object.
var jsonString = [{"productid":"17","product_quantity":"2"}, {"productid":"9","product_quantity":"1"}];
var data = JSON.parse(jsonString);
console.log(data[1].productid); // 9
But I do not know what you mean by."Now I want to separate every value in different variable."
Why?
Anyways you could do this. Though I do say dont. But you asked.
data.forEach(function(item){
window["productid"+item.productid] = item.product_quantity;
});
will give you 2 vars in global scope
console.log(productid17); // "2"
console.log(productid9); // "1"

Categories

Resources