0
[
  {
    "orderDetails": [
        {
            "account_name": "akhil_kotak",
        }
     ]
  }
]

How to get the account name from this json, i tried doing this

String response = new String(responseBody);
                    //ON SUCCESS GETS JSON Object
                    JSONArray array = new JSONArray(response);
                    JSONObject obj = 
array.getJSONObject(0).getJSONArray("orderDetails").getJSONObject(0);

                    txt_accountName.setText(obj.getString("account_name"));

If anyone can help, that would be awesome.

Thanks

4 Answers4

0

Your JSON is invalid.

You can change to this.

[
  {
    "orderDetails": [
        {
            "account_name": "akhil_kotak"
        }
     ]
  }
]

Just change "account_name": "akhil_kotak" , to "account_name": "akhil_kotak" .

Just remove the comma in your JSON .

Try this .

try {
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray orderDetails = jsonObject.getJSONArray("orderDetails");
            for (int j = 0; j < orderDetails.length(); j++) {
                JSONObject jsonObject2 = orderDetails.getJSONObject(j);
                String account_name = jsonObject2.getString("account_name");
                txt_accountName.setText(account_name);
            }
        }
} catch (JSONException e) {
        e.printStackTrace();
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

try this. i am using org.json.simple library.

  String str="[{\"orderDetails\": [{\"account_name\": \"akhil_kotak\",}]}]";
            JSONParser parser=new JSONParser();
            JSONArray array=(JSONArray) parser.parse(str);
            JSONObject obj=(JSONObject) array.get(0);
            JSONArray nestedArray=(JSONArray) obj.get("orderDetails");
            System.out.println("output: "+nestedArray.get(0).get("account_name"));

it's working fine.

gajju_15
  • 527
  • 4
  • 17
0

Your JSON is invalid. Remove comma! then:

Create a set of calsses and use Gson!

Add this line to dependencies in your build.gradle:

dependencies {
    compile 'com.google.code.gson:gson:2.8.2' //add this line
}

Crate a set of classes:

class SomeListItem {
    @SerializedName("orderDetails")
    public List<InnerListItem> mOrderDetails;
}

class InnerListItem {
    @SerializedName("account_name")
    public String mAccountName;
}

And use them like this:

String jsonOutput = "[\n" +
        "  {\n" +
        "    \"orderDetails\": [\n" +
        "        {\n" +
        "            \"account_name\": \"akhil_kotak\"\n" +
        "        }\n" +
        "     ]\n" +
        "  }\n" +
        "]";
Gson gson = new Gson();
Type listType = new TypeToken<List<SomeListItem>>(){}.getType();
List<SomeListItem> parsedJson = gson.fromJson(jsonOutput, listType);

String accountName = parsedJson.get(0).mOrderDetails.get(0).mAccountName;
Andrey Busik
  • 441
  • 3
  • 16
0

if you are using Jackson 2 .. below works

String jsonString = "[{\"orderDetails\": [{\"account_name\": \"akhil_kotak\",}]}]";

ObjectMapper mapper = new ObjectMapper();
JsonNode jnNode= mapper.readTree(jsonString);
Sting accName=jnNode.get("orderDetails").get("account_name").asText();
}
APR
  • 11
  • 1
  • 1
  • 3