我从服务器返回了一些 JSON:
{
"outer" : {
"inner" : "1"
},
"outer2" : {
"inner" : "1"
},
"outer3" : {
"inner" : "1"
},
}
我想以编程方式访问 ~something~.inner,并且我有一个包含该数据的变量,但如果我说 variable.inner,它就不起作用(显然)。这样做的正确语法是什么?
谢谢!
我从服务器返回了一些 JSON:
{
"outer" : {
"inner" : "1"
},
"outer2" : {
"inner" : "1"
},
"outer3" : {
"inner" : "1"
},
}
我想以编程方式访问 ~something~.inner,并且我有一个包含该数据的变量,但如果我说 variable.inner,它就不起作用(显然)。这样做的正确语法是什么?
谢谢!
data = JSON.parse(jsonString);
var yourvar = 'outer';
data[yourvar].inner;
假设 javascript
评估({“外部”:{“内部”:“1”},
“外部2”:{“内部”:“1”},
“外部3”:{“内部”:“1”},
}).outer.inner
在javascript中
You should use the code below to easily access nested JSON objects.
// http://json-simple.googlecode.com/files/json-simple-1.1.1.jar
import org.json.simple.*;
import org.json.simple.parser.*;
/*
* "Simpler" version of simple JSON. Easy to use interface for easy parsing of JSON nested objects.
*
* Usage:
* Json json = new Json("{roster: [{id: 1, name: \"Adam\"}, {id: 2, name: \"Eve\"}]}");
* JSONArray arr = json.getArray("roster");
* JSONObject obj = json.getObject("roster[0]");
* int id = json.getInt("roster[0].id");
* String name = json.getString("roster[1].name");
*
*/
public class Json {
private JSONParser m_parser = new JSONParser();
private JSONObject m_json = null;
private String m_strErrMsg = "";
public Json(String str) {
reset();
m_json = parse(str);
}
public Json(JSONObject obj) {
reset();
m_json = obj;
}
public JSONObject parse(String str) {
setErrMsg("");
JSONObject obj = null;
try {
if (m_parser != null) {
obj = (JSONObject)m_parser.parse(str);
}
}
catch (Exception ex) {
setErrMsg("Exception occurred while parsing json string. Msg: " + ex.toString());
return obj;
}
return obj;
}
public String getErrMsg() { return m_strErrMsg; }
public void setErrMsg(String str) { m_strErrMsg = str; }
public void clearErrMsg() { m_strErrMsg = ""; }
public void reset() {
m_json = null;
m_strErrMsg = "";
}
public Object get(String path) {
return get(m_json, path);
}
public JSONObject getObject(String path) {
Object obj = get(path);
return (obj != null ? (JSONObject)obj : null);
}
public JSONArray getArray(String path) {
Object obj = get(path);
return (obj != null ? (JSONArray)obj : null);
}
public String getString(String path) {
Object obj = get(path);
return (obj != null ? obj.toString() : "");
}
public int getInt(String path) {
int val = 0;
try {
String str = getString(path);
val = Integer.parseInt(str);
}
catch (Exception ex) {
return 0;
}
return val;
}
public long getLong(String path) {
long val = 0L;
try {
String str = getString(path);
val = Long.parseLong(str);
}
catch (Exception ex) {
return 0L;
}
return val;
}
public double getDouble(String path) {
double val = 0.0;
try {
String str = getString(path);
val = Double.parseDouble(str);
}
catch (Exception ex) {
return 0.0;
}
return val;
}
public boolean getBool(String path) {
boolean val = false;
try {
String str = getString(path);
val = Boolean.parseBoolean(str);
}
catch (Exception ex) {
return false;
}
return val;
}
// path: "roster[0].name"
public Object get(JSONObject obj, String path) {
if (obj == null || path == null) {
return null;
}
path = path.trim();
if (path.isEmpty()) { return null; }
int index = path.indexOf('.');
if (index > 0) {
String key = path.substring(0, index).trim();
path = path.substring(index + 1).trim();
JSONObject inner = (JSONObject)get(obj, key);
return get(inner, path);
}
else if (path.endsWith("]")) {
int arrayIndex = -1;
int open = path.indexOf('[');
int close = path.indexOf(']');
if (open > 0 && close > 0 && (close - open) > 1 && close == path.length() - 1) {
try {
arrayIndex = Integer.parseInt(path.substring(open + 1, close));
}
catch (Exception ex) {
arrayIndex = -1;
}
}
if (arrayIndex >= 0) {
path = path.substring(0, open).trim();
JSONArray array = (JSONArray)obj.get(path);
if (array != null && array.size() > arrayIndex) {
return array.get(arrayIndex);
}
else {
return null;
}
}
else {
// path is key
return obj.get(path);
}
}
else {
// path is key
return obj.get(path);
}
}
}