JSON select from array - javascript

I have the following JSON object:
[{"_id":"57b1f03ec4f37923cc436100","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}},
{"_id":"57b1f03fc4f37923cc436101","pokedex_id":134,"location":{"type":"Point","coordinates":[-117.4955084,34.0175467]}}]
I'm trying to access the data with console.log(temp[0]);, but for some reason my output is just [. What am I doing wrong? Also, any subsequent index just seems to return the corresponding char value. For example: temp[1] returns {, temp[3] returns _.

In JavaScript, there is no such thing as a JSON object.
You have a JSON text, which is a string.
somestring[0] gives you the first character of that string.
Use JSON.parse() to convert your JSON text into a JavaScript array.

It's because it's stringified right now.
Accessing 'somestring'[0] will give you the first character of the string.
To cast your JSON string to a plain old JavaScript object, you should do
var actualArray = JSON.parse(temp);
Then, actualArray[0] will give you the first item in the array.

Another way is to parse your JSON file into a string, and retrieve the values.
String JSONFileString = File.ReadAllText("yourFile.json");
JavaScriptSerializer slizer = new JavaScriptSerializer();
ObjectName o1 = slizer.Deserialize<ObjectName>(JSONString);
Console.Writeline(o1);

Related

Convert a string made from array toString() back to Array

I converted an array to a string and added it to a TextArea. The user edited the TextArea and I need to now update the array by calling in the same string of data I first produced. How would you do this?
The string which I have produced and need to convert back to an array is:
{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}
I tried to use the JSON Parser JSON.parse(text)
Format your string:
const text = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}'
console.log(JSON.parse(`[ ${text}]`))
You just need to format your string as an array in JSON format. You can do that like so:
JSON.parse('[' + text + ']')
The Below code should work:
var text_string = '{"color":"red","x":218,"y":-11,"width":60,"height":60},{"color":"blue","cx":114,"cy":83,"radius":30}';
console.log(JSON.parse(`[${text_string}]`));

How do JSON.parse() and escape characters work in JavaScript?

Suppose I have an object variable:
var obj = {
key: '\"Hello World\"'
}
Then I tried parse it to string by using JSON.stringify in Chrome devtools console:
JSON.stringify(obj) // "{"key":"\"Hello World\""}"
I get the result "{"key":"\"Hello World\""}". Then I give it to a string
var str = '{"key":"\"Hello World\""}'
At least I try to convert it back to obj:
JSON.parse(str);
but the browser tell me wrong Uncaught SyntaxError
What confused me is why this is wrong? I get the string from an origin object and I just want turn it back.
How can I fix this problem? If I want do the job like convert obj to string and return it back, how can I do?
You're tried to convert your JSON into a string literal by wrapping it in ' characters, but \ characters have special meaning inside JavaScript string literals and \" gets converted to " by the JavaScript parser before it reaches the JSON parser.
You need to escape the \ characters too.
var str = '{"key":"\\"Hello World\\""}'
That said, in general, it is better to not try to embed JSON in JavaScript string literals only to parse them with JSON.parse in the first place. JSON syntax is a subset of JavaScript so you can use it directly.
var result = {"key":"\"Hello World\""};
try:
var str = '{"key":"\\"Hello World\\""}';

Convert array formatted strings to object

I got
[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]
this from google translator
However I tried
JSON.parse(xhr.responseText);
It return an error Unexpected token
The problem is that this string contains multiple commas making your json invalid.
You could try to replace it for a single one before parsing
var x = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]'
.replace(/,{2,}/g, ",") // 2 or more replace for 1
JSON.parse(x);
Or if you have access to whatever is sending this string fix the output.
First you should remove an extra [] brackets by replacing that.
ex,
[["汽車","car","Qìchē",""]]
should be:
["汽車","car","Qìchē",""]
EDIT: you can refer to this answer: Parse Google Translate Json C#
You should try:
var str = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]';
var objstr = $.parseJSON(str);

How are strings physically stored in Javascript

What I am looking for is how strings are physically treated in Javascript. Best example I can think of for what I mean is that in the Java api it describes the storage of strings as:
String str = "abc";" is equivalent to: "char data[] = {'a', 'b', 'c'};
To me this says it uses an array object and stores each character as its own object to be used/accessed later (I am usually wrong on these things!)...
How does Javascript do this?
Strings are String objects in JavaScript. The String object can use the [] notation to get character from a string ("abc"[0] returns 'a'). You can also use the String.prototype.charAt function to achieve the same result.
Side node: var a = 'abc' and var b = new String('abc') are not the same. The first case is called a primitive string and get converted to a String object by the JavaScript parser. This results in other data types, calling typeof(a) gives you string but typeof(b) gives you object.
Strings are stored in the same format in javascript as other languages stores.
Suppose var word = "test" than at word will be as an array of characters and the 't' will come at 0th position and so on.
The last iteration as taking 'word.length' will return undefined. In other languages, it returns as '\0'.

How to get value after a particular string in an array in javascript

I have a json data in this format.
({test:[test, "<br/><div><b>Name</b>&nbsp;&nbsp;&nbsp; James;/div><div><b>Company</b>&nbsp;&nbsp;&nbsp;Company1</div><div><b>Phone.....}).
I need to get the value after 'Company' using javascript, is there any possibilities....
Please help me on this....
assuming your data was in some sort of parsable format, you could do this:
var textAfterCompany = data.split('Company')[1];
split returns an array of strings, [0] is everything to the left and [1] is everything to the right (assuming there is only one occurrence of 'company')
var text = test[1].substring(test[1].indexOf("Company"), test[1].length);

Categories

Resources