How to convert string to JSON object? - javascript

I am using jquery 1.3.2 and I can't use the parseJSON since it is added in 1.4.1 & I can't upgrade right now.
Is there any wat to get JSON object from string?
If this is a duplicate please send me the link.
Thanks for reading

You can use a couple of options, see here
Javascript eval function
JSON.parse() function
The eval function has some security issues associated with it, and the JSON.parse function has its own set of incompatibilities. I guess this is why JQuery has its own wrapper function. If you can't use JQuery 1.4+ though, these are two of your best options, without going looking for a specific JSON parsing library.

json2.js : http://www.json.org/js.html

Related

How can I parse a JSON file with comments in client-side Javascript? (Don't need to preserve comments.)

First, I know that JSON doesn't support comments. I'm using them anyway because I need to write little notes to make it easy for people who aren't tech-savvy to edit this file.
I'm using double-slash comments, but I can switch to slash-star if needed.
"campaignID": "230109-stock-shop", // this is the method I want
I know the technique of creating extra comment fields like this, but it doesn't fit my needs.
"__comment": "this ISN'T the method I want"
I'm working entirely in client-side Javascript/jQuery. When I import a file with comments, it - of course - doesn't work.
$.getJSON('file.json', function (json) {//do things})
Is it possible to strip out the comments when importing the JSON file somehow? I tried to find a JS library or something that would do that, but everything I found is server-side, and I need everything to run client-side.
I'm also not super great with Javascript, so it's possible I'm missing something obvious.
I really appreciate any help you can give!
You can remove all the comments before trying to parse it.
You won't be able to use $.getJSON(), since that tries to parse the JSON itself. Use $.get(), remove the comment and parse it.
$.get('file.json', function(commented_json) {
json = JSON.parse(commented_json.replace(/\/\/.*/g, ''));
// do things
}, "text");
Note that this won't work properly if any of the strings in the JSON contain //. That would require using a real JavaScript parser so it can distinguish the context.

jQuery.data('itemname') not working but jQuery.attr('data-itemname') is working

This is weird behavior but when I am trying to access some data attribute using jQuery data() function, it is returning me undefined but attr() is returning me the actual value. I know data works differently as it caches for the first time and then it retrieves from cache. I am using jQuery 1.4.2 and then tried looking with jQuery 1.7.1 and it did work. But I can't upgrade to 1.7.1 at the moment. Any thoughts on it??
I will appreciate any kind of suggestion or help provided
HTML5 data-* objects being pulled into .data was not done until jQuery 1.4.3: http://api.jquery.com/data/#data-html5
Maybe you can just update to 1.4.3?
You answered your own question: data doesn't work in jQuery 1.4.2. If you can't upgrade, you try this plugin.

YUI Datatable to JS object

I have a problem. I have YUI DataTable and i need convert it to JS object. But I'm not found standart ways for this. If anyone know, how to solve this problem - please answer.
Thank you.
Since YUI 3.5.0 you just need to do datatable.get('data').toJSON(). datatable.get('data') now returns a Y.ModelList which in turn has a toJSON method.

How to access JSON from an iframe originating from same domain?

In my webpage a hidden iframe is loaded with some JSON in it. This JSON is refreshed by some actions on the page. How do I access this JSON in iframe from my web page? for some unknown arcane unexplainable reason I am forced to use jQuery 1.3.2. so no $.parseJSON()
I think you can use:
var json = $.parseJSON($("#hiddeniframe").contents().text());
Something along those lines will work at least.
All modern browsers include a JSON parsing library:
var data = JSON.parse($("#hiddeniframe").contents().text());
If you need to support older browsers there are several libraries to choose from that will provide the same interface. The better ones will check to see if the browser is providing a native implementation and not override it, since it's bound to be faster.
See also JSON.stringify()
The code #Paulpro posted:
var json = $.parseJSON($("#hiddeniframe").contents().text());
doesn't work for me.
I changed the code to:
var json = $.parseJSON($("#hiddeniframe").contents().find("*").first().text());
And now it works.

How do you Serialize ScriptObjects to JSON to save in Silverlight Isolated Storage?

According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when I try to use it VS says
"Type 'DataContractJsonSerializer' is not defined".
I have a method marked as ScriptableMember that gets called from JavaScript and is passed an Object. Inside this method I need to serialize the object to a string (preferably JSON) and then save it in isolated storage.
Does Silverlight 2 Beta 2 really support DataContractJsonSerializer? Or would anyone recommend a different method of saving the JavaScript created ScriptObject in the Isolated Storage?
Actually the answer is, the DataContractJsonSerializer is part of Silverlight 2 Beta 2, but you need to add a reference to System.ServiceModel.Web to your Silverlight project to use it.
I didn't realize that you still needed to add dll references in Silverlight. I thought it automatically included everything in a similar way to how ASP.NET does.
There is a Silverlight version of Json.NET that will serialize your objects to JSON. It doesn't require [DataContract] and [DataMember] attributes all over your objects.
Json.NET
For now, the only solution to this that I have found is to use the ASP.NET AJAX JavaScriptSerializer to do the JSON serialization/deserialization in JavaScript, and then just use Silverlight to store/retrieve the resulting string.
Sys.Serialization.JavaScriptSerializer.serialize(obj);
Sys.Serialization.JavaScriptSerializer.deserialize(json);
I would say your own answer would be the best approach. JavaScript is dead slow at doing stuff like that, so best you leave the serialization-part to ASP.NET.

Categories

Resources