我将一些JSON数据保存在assests文件夹中的txt文件中。然后我正在读取txt文件并将结果保存在字符串中。现在我正在尝试将字符串转换为JSONObject并从每个键获取一些数据。以下是代码。
========从文件中读取的方法:
select
s.id as store_id,
st.product_id,
st.start_date,
st.quantity
from
stores s
left join stock st on s.Id = st.store_id and st.product_id = 407214
=======并且在onCreate()中:
private String readMyJsonFile()
{
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("myFile.txt"), "UTF-8"));
mLine = reader.readLine();
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
return mLine;
}
catch(JSONException e){e.printStackTrace(); }
=============================================== = 并且JSON数据是:
String JsonStr = readMyJsonFile();
try {
JSONObject jsonObj = new JSONObject(JsonStr);
JSONObject questionMark = JsonObj.getJSONObject("structure_details");
Iterator keys = questionMark.keys();
while(keys.hasNext())
{
String currentDynamicKey = (String)keys.next();
JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);
}
}
=============================================== ===========
我得到了结果,但问题是我没有在 JSONObject jsonObj = new JSONObject(JsonStr); 中连续获取JSONObject。该序列与 JsonStr 不同。
如何解决?
答案 0 :(得分:1)
在字符串的ArrayList中获取迭代器键。然后循环遍历每个arraylist对象并获取JsonObjects。以下是可能有用的代码:
PropertyChangeEvent
答案 1 :(得分:0)
不是JSON:
"structure_details":{"x1":{"id":"54","name":"sh"},
"x2":{"id":"69","name":"dd"},
"x3":{"id":"80","name":"kk"}
}
是JSON:
{"structure_details":{"x1":{"id":"54","name":"sh"},
"x2":{"id":"69","name":"dd"},
"x3":{"id":"80","name":"kk"}
}}
和它JSON:
{"x1":{"id":"54","name":"sh"},
"x2":{"id":"69","name":"dd"},
"x3":{"id":"80","name":"kk"}
}