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 this Json file located in different folder than my js file:
{"Title":"test1", "content":"test2"}, {"Title":"test1", "content":"test2"}
I want to read this file and turn the json into a JavaScript array of objects.
I can use jQuery.
Here you go: http://jsfiddle.net/p3p5P/
Use jQuery.getJSON to load JSON-encoded data from the server using a GET HTTP request. Then use jQuery.parseJSON to takes a well-formed JSON string and return the resulting JavaScript object.
//$.getJSON("yourJsonFile.json", function(myJson) {
//console.log(json); // this will show the info in the console
//hard-code the json for this fiddle example, you will load it with the getJSON statement above
var myJson = '[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]';
var myJsonObj = $.parseJSON(myJson);
console.log(myJsonObj); //you now have an array of objects.
alert(myJsonObj[0].Title); //how to reference the first title
alert(myJsonObj[1].Title); //how to reference the second title
alert(myJsonObj[0].content); //how to reference the first content
alert(myJsonObj[1].content); //how to reference the second content
//});
NOTE: In this example I turned your json data into an array by enclosing the entire string within [ ]. So you will want to edit your json and change this...
{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}
into this...
[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]
Use jQuery.getJSON to get Json from file,
and then use jQuery.parseJSON to transform into a JS array.
$.getJSON( "ajax/test.json", function( data ) {
var jsarray = jQuery.parseJSON(data);
});
Related
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
I received data from another API. But I don't know how to convert and use data.
data example
{
...
answer:'List(value1,value2,value3,value4)',
...
}
I want to iterate all answer nodes. please help.
Approach
You could capture the group between List(...) and split that by comma ,
Regex test: https://regex101.com/r/xFf39R/1
const answer = 'List(value1,value2,value3,value4)'
const res = /List\((.*)\)/.exec(answer)[1].split(',')
console.log(res)
Reference
RegExp.prototype.exec()
Return value
[...] The returned array has the matched text as the first item, and then one item for each parenthetical capture group of the matched text.
Reading your data example... It seems like the answer is carrying a String as the single quote is presented in your data example.
answer:'List(value1,value2,value3,value4)'
^ ^
And the List(value1,value2,value3,value4) actually looks like some python List in system print.
Well, but this does not help in your direct question.
Assuming your want to get all four values into an array in javascript, do the followings
let data_example = {
...
'answer':'List(value1,value2,value3,value4)',
...
}
let answer_string = data_example['answer'].slice(5,-1)
let your_list = answer_string.split(',')
console.log(your_list)
//["value1","value2","value3","value4"]
But be careful... I assume your List() always start with 'List(' and end with ')'
See more on slice and split
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 3 years ago.
Improve this question
I need to console log my anser and not just return it
I have tried this however in my console it does not LOG it but returns (3) ["JavaScript", "CSS", "HTML"]
I need it to return
* HTML,CSS,JAVASCRIPT
function logReverse(input) {
let arr = input.reverse();
console.log(arr)
}
//test
logReverse(['HTML', 'CSS', 'JavaScript'])
Please help out
When you run code in the console, return values are displayed as well as any console output. What you should see in the console when you run that code is:
Array(3) [ "JavaScript", "CSS", "HTML" ] - this is the console.log, logging the object you have passed it
undefined - this is the return value of the function
When you said you need it to return HTML,CSS,JavaScript, I assume what you actually want to do is to take a string value of the reversed array, which would look like this:
console.log(Array(...input).reverse().toString())
Note that I've wrapped the input in Array(...input), which creates a new array. Array.prototype.reverse will reverse the array in place, which means that it changes the value of reverse. By making a new array, you can avoid modifying the input.
The call to Array.prototype.toString will join the elements with commas, which I believe is the desired effect you're after.
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 3 years ago.
Improve this question
I am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))
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 3 years ago.
Improve this question
data:
{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test
message on line1\\ntest message on
line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}
It unfortunately comes in as a string, though I'd like it to change into a dict if possible.
use json library in python
import json
json.loads(your_string)
You can use the function json.loads() which parse the string in to JSON object(Python dictionary) by importing the json library.
Code :
import json
json_string = '{\"id\":123,\"channel\":\"private_A25BHd\",\"text\":{\"content\":\"{\\"m\\":\\"event\\",\\"p\\":{\\"id\\":123123123,\\"aid\\":125123123,\\"sym\\":\\"BITMEX:XBTUSD\\",\\"res\\":\\"1\\",\\"desc\\":\\"test message on line1\\ntest message on line2\\",\\"snd\\":false,\\"snd_file\\":\\"alert\/fired\\",\\"snd_duration\\":0.0,\\"popup\\":true,\\"fire_time\\":123123,\\"bar_time\\":123123,\\"cross_int\\":true}}\",\"channel\":\"alert\"}}'
parsed_string = json.loads(json_string)
print(parsed_string)
Output :
{'text': {'channel': 'alert', 'content': '{"m":"event","p":{"id":123123123,"aid":125123123,"sym":"BITMEX:XBTUSD","res":
"1","desc":"test message on line1\ntest message on line2","snd":false,"snd_file":"alert/fired","snd_duration":0.0,"popu
p":true,"fire_time":123123,"bar_time":123123,"cross_int":true}}'}, 'channel': 'private_A25BHd', 'id': 123}
& if you want to simply print any specific value, you can use
print(parsed_string["id"])
it prints the id value which is 123
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 6 years ago.
Improve this question
Commonly, arrays are formed like var array = ["some","string","elements"] or var array = [1, 2, 3]. Been searching for times, but I haven't seen articles that may clear my mind. Now, is it possible to have an array structure like var array = [some, string, elements]. As what can be observed, it was somehow a string without "".
To visualize my concern, I have this code
var data = []
for (var x = 0; x < arrayData.length; x++){ //arrData contains [1,2] structure
data.push("$scope.item["arrayData[x]"]");
}
//which gives var data = ["$scope.item[1], $scope.item[2]"]
I needed that var data to form like var data = [$scope.item[1],$scope.item[2]]. Is it possible?
EDIT
My bad, I haven't explained my query fully. The "$scope.item[" is a string, that's why I encapsulated it to ""
EDIT II
Is it possible to have an array structure like var array = [some, string, here]. Consider that some,string and here are not variables.
Don't complecate it, Just go with using JSON.stringify();
Your code should be
data.push(JSON.stringify("$scope.item[" + arrayData[x] + "]"));
You cannot have an array structure like var array = [some, string, elements]. There should be variables named some, string and elements.
$scope is a special variable in AngularJS. So, you should use it without "s. For example:
data.push($scope.item[arrayData[x]]);