繁体   English   中英

JSON数组解析多个数组

[英]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_timepurpose您可以执行以下操作:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM