简体   繁体   中英

JSON Array Parsing multiple array

i have this fragment of JSON code that i want to parse: basically i want to store the "effective time" and "purpose", that you can see inside the "results" json array using Java(Android Studio), but i'm struggling doing it as it's my first time dealing with 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."
          ]
              ...
              ...
        }
       ]
 }

this is my code so far

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 is still showing "no description"

thank you for the help!

If you are new you should go through some tutorial on Json parsing here .

For getting the effective_time and purpose you can do as:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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