[英]JSON Array Parsing multiple array
我有要解析的JSON代碼片段:基本上,我想存儲“有效時間”和“目的”,您可以使用Java(Android Studio)在“結果” json數組中看到,但我是這是我第一次處理JSON,因此很努力。
{
"results": [
{
"effective_time": "20121114",
"inactive_ingredient": [
"Inactive ingredients *acetylated monoglycerides, *anhydrous lactose, *carnauba wax, colloidal silicon dioxide,*corn starch, *croscarmellose sodium, D&C Yellow #10 Aluminum Lake, FD&C Yellow #6 Aluminum Lake, hypromellose, *hypromellose phthalate, *iron oxide Yellow (iron oxide ochre), methacrylic acid copolymer, microcrystalline cellulose, *mineral oil, *polyethylene glycol (PEG)-400, *polysorbate 80, povidone, pregelatinized starch, *propylene glycol, *simethicone, silicon dioxide, sodium bicarbonate, sodium hydroxide, sodium lauryl sulfate, starch, stearic acid, talc, titanium dioxide, triacetin, and triethyl citrate. *May also contain."
],
"purpose": [
"Purpose Pain reliever"
],
"keep_out_of_reach_of_children": [
"Keep out of reach of children In case of overdose, get medical help or contact a Poison Control Center right away."
]
...
...
}
]
}
到目前為止,這是我的代碼
String drugDescription="no description";
try{
JSONObject jsonQueryResult = new JSONObject(JSONFILE);
JSONArray jsonResultArray = jsonQueryResult.getJSONArray("result");
JSONObject jsonDrugDescription = jsonResultArray.getJSONObject(0);
drugDescription = jsonDrugDescription.toString();
}
catch(JSONException e){
e.printStackTrace();
}
searchResultTextView.setText(drugDescription);
drugDescription仍顯示“無描述”
感謝您的幫助!
如果您是新手,則應在此處閱讀有關Json解析的一些教程。
為了獲得effective_time
和purpose
您可以執行以下操作:
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray firstResult = jsonObject.getJSONArray("results");
if (firstResult != null && firstResult.length() > 0) {
for (int i=0; i<firstResult.length(); i++) {
JSONObject result = firstResult.getJSONObject(i);
// This is your effective_time;
String effective_time = result.getString("effective_time");
JSONArray purpose = result.getJSONArray("purpose");
if (purpose != null && purpose.length() > 0) {
for (int j=0; j<purpose.length(); j++) {
// This is the purpose;
String purposeData = purpose.getString(j);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.