【问题标题】:JSON Parsing and storing them in variablesJSON解析并将它们存储在变量中
【发布时间】:2016-09-23 22:40:49
【问题描述】:
我正在使用 Wamp 服务器创建一个数据库,我已经能够在 android studio 中以 JSON 格式检索它们,但我想将它们存储在变量中,并且 WAMP 中的数据库需要经常更新,所以我想要要存储的数据是一个变量
{
“服务器响应”:[
{
“泵”:“Sajha”,
“可用”:“1”
},
{
“泵”:“Bhadrakali”,
“可用”:“0”
},
{
“泵”:“Kumaripati”,
“可用”:“0”
},
{
“泵”:“巴尔库”,
“可用”:“1”
}
]
}
即,我想在 android studio 中某个泵的可用为 1 时执行某个任务,而当它为 0 时执行不同的任务,我该怎么做,请有人给我发代码。
【问题讨论】:
标签:
sql
json
android-studio
wamp
【解决方案1】:
首先将输出 json 转换为 JSONObject 并通过子对象创建一个数组。再次将孩子转换为 Json 对象并提取数据:
String parentObject= new JSONObject(output);
String pumps= parentObject.optString("server_response").toString();
JSONArray childrenArray = new JSONArray(pumps);
for(int i=0; i < childrenArray.length(); i++)
{
JSONObject childObject = childrenArray.getJSONObject(i);
String Pump= childObject.optString("Pump").toString();
String Available= childObject.optString("Available").toString();
//if (Available.equals("1")){Do something}
}
你也可以直接使用getString而不是optString,如果你确定对象不为空,那么你不需要转换它toString()