황현정
황현정

Reputation: 3521

How to get value from JSON?

Hmmm.. I have this json values:

var form = {
lat: event.row['lat'].value,
lon: event.row['lon'].value,
}

Android.openForm( $.toJSON(form) );

How do I get the value from lat and long?

openForm: function( json ){
    alert(json[lat]);
    //$('#lat').val(json.lat);
}

Upvotes: 1

Views: 2467

Answers (6)

hugomg
hugomg

Reputation: 69924

Why don't you have the openForm receive the object directly instead of its json serialization?

openForm(form){
    var json = $.toJSON(form);
    alert(form.lat);
}

Upvotes: 2

Scruffy The Janitor
Scruffy The Janitor

Reputation: 472

If the results of form is to be this

var form = {
lat: "somevalue",
lon: "somevalue"
};

You would access the data in the variable form by the dot properties.

form.lat and form.lon

Simple Fiddler

Upvotes: 2

RightSaidFred
RightSaidFred

Reputation: 11327

If $.toJSON is a method like JSON.stringify, then you've serialized the data into JSON text, so it's values are no longer available via properties.

You'd need to parse it first.

openForm: function( json ){

     // UPDATED to use the JSON plugin you've loaded
    var parsed = $.evalJSON( json ); 

    alert( parsed[lat] );
}

I assume that the original form variable is not accessible from where you're trying to retrieve the value, otherwise you probably wouldn't serialize it in the first place.

If the original form data is in the same execution environment, and could be accessed directly from your function, you should do that instead of serializing.


What is JSON, and why do we use it?

For those who don't understand what JSON is meant for, it is a text based serialization format used to transfer data between environments where data can not naturally be shared.

The serialization is simply a standardized format that gets "stringified" from the native data structures of one environment, and then parsed into the native data structures of a different environment.

I assume toJSON is the "stringify" function, and the openForm is the separate environment into which the JSON data has been transferred.

If these assumptions are correct, the JSON needs to be parsed into the new environment before its values can be easily accessed.

Upvotes: -1

황현정
황현정

Reputation: 3521

Didn't realize that my team mate was using a plugin... http://code.google.com/p/jquery-json/

I should have asked him first.. @_@ Sorry neh

var thing = {plugin: 'jquery-json', version: 2.3};

var encoded = $.toJSON( thing );
// '{"plugin":"jquery-json","version":2.3}'
var name = $.evalJSON( encoded ).plugin;
// "jquery-json"
var version = $.evalJSON(encoded).version;
// 2.3

Upvotes: 0

Razvan Pat
Razvan Pat

Reputation: 21

$.toJSON(form) converts your object to a string, I think you want to pass the object so just drop it:

Android.openForm(form);

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this

openForm: function( json ){

    var lat = json.lat;//or json["lat"]
    var lon = json.lon;//or json["lon"]

}

By the way variable form is already a well formed json you don't have to convert it to json in order to use it.

Upvotes: -1

Related Questions