My JScript is:
var t={'color':'red'}; // dynamic json data.
for(n in t)
{
alert(n)
}
here, alert gives the json key color. but how to get its value?
Note: the json is dynamic.
var t={'color':'red'}; // dynamic json data.
for(n in t)
{
alert(n);// n = key
var val =t[n];// value where key is n
}
Here is a simple example to get dynamic keys from json response - Get dynamic keys from JSON Data
public void getData(String data){
// Load json data and display
JSONObject jsonData = new JSONObject(data);
// Use loop to get keys from your response
Iterator itr = jsonData.keys();
while(itr.hasNext()){
String keys = (String)itr.next();
Log.e("Keys", "----"+keys);
JSONArray dynamicValue = jsonData.getJSONArray(keys);
// Your stuff here
} }
instead of putting the n in al alert put it in an external variable or something...
Edited, try sometnihg like this:
var ex_n;
var t={'color':'red'};
for(var i=0; i<t.length; i++) ex_n = t[i]["color"];