0

I want to get key from json object. I have used the below code to get it,

JSONParser parser = new JSONParser();
   try {

                Object obj = null;
                try {
                    obj = parser.parse(new FileReader(new File("json/customer_list.json")));
                } catch (org.json.simple.parser.ParseException e1) {
                    e1.printStackTrace();
                }
                JSONObject jsonObject = (JSONObject) obj;
                JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
                for (int i = 0; i < listOfBranches.size(); i++) {
                    System.out.println("Customer :" + listOfBranches.get(i));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

In this part,

System.out.println("Customer :" + listOfBranches.get(i));

I'm getting the below object,

{"Photo":{"input_required":false,"output_type":"","input_fields":[{"input_type":"INTEGER","length":"","reg_exp":"","label":"","field_type":"TEXT"},{"values":"","length":"","reg_exp":"","label":"Mode","field_type":"drop_down"},{"length":"","input_type":"INTEGER","reg_exp":"","label":"Quantity","field_type":"TEXT"}]},"video":{"input_required":true,"output_type":"","input_pattern":""},"drawing":{"input_required":true,"output_type":"PDF","input_pattern":""}}

from this json i want to get photo, video and drawing key. Can you please suggest me any idea to do this? Thanks in advance.

Balasubramanian
  • 700
  • 7
  • 26
Olive
  • 149
  • 1
  • 4
  • 13
  • Normally you can get a JSON value using something like `output["Photo"]`, but I don't know if that works in Java. Would `listOfBranches.get("Photo")` work? – numbermaniac Oct 31 '17 at 05:00

1 Answers1

-2

I have get key from json object using the below code,

try {

            Object obj = null;
            try {
                obj = parser.parse(new FileReader(new File("json/customer_list.json")));
            } catch (org.json.simple.parser.ParseException e1) {
                e1.printStackTrace();
            }
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
            for (int i = 0; i < listOfBranches.size(); i++) {
                JSONObject item = (JSONObject)listOfBranches.get(i);
                Set keys = item.keySet();
                Iterator a = keys.iterator();
                while(a.hasNext()) {
                    String key = (String)a.next();
                    System.out.print("key : "+key);
                }
                System.out.println("Customer :" + listOfBranches.get(i)+"\n item "+item);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Happy Coding...

Olive
  • 149
  • 1
  • 4
  • 13
  • 2
    Why you have asked question if you already know the answer? Have you tried searching it before asking the question here? – Janki Gadhiya Oct 31 '17 at 05:18
  • @JankiGadhiya Yes I have searched and i'm not getting answers. Thats y posted question. And I have tried with above answer, its works for me. So it may help others like me who getting trouble in this same problem. Thats why posted answer. why should I post questions even thought know the answer? probably it waste my time. So i would not prefer that. – Olive Oct 31 '17 at 05:23