1
[
 {
  "name": "Test",
  "type": "Private",
  "item":[{"itmeNo":"PT-15003C","quantity":"3"},
            {"itmeNo":"PT-15003C","quantity":"3"}],

  "successMsg":"Item(s) added to the job list."
  }  
]

Hi, I am doing data parameterization using json.Above is my json data, i want to enter this data in one form wherein i need to enter itemNo and quantity together. How can that be done using data parameterization using json. When there is one key-one value pair then my code is working but in this case can anyone please help me getting solution ? I have written following code for single key pair value.

 public static Object[][] getData(String path) {
    JSONParser parser = new JSONParser();
    JSONArray jArray = null;
    Object[][] testData = null;
    try {
        jArray = (JSONArray) parser.parse(new FileReader(System.getProperty("user.dir") + path));
        testData = new Object[jArray.size()][1];
        Hashtable<String, String> table = new Hashtable<String, String>();
        int i = 0;
        for (Object obj : jArray) {
            table = new Hashtable<String, String>();
            JSONObject objJson = (JSONObject) obj;
            Set<?> keys = objJson.keySet();
            Iterator a = keys.iterator();
            while (a.hasNext()) {
                String key = (String) a.next();
                String value = (String) objJson.get(key);
                // System.out.print("key : "+key);
                // System.out.println(" value :"+value);
                table.put(key, value);
            }
            testData[i][0] = table;
            i++;
        }
        return testData;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
Bharat
  • 2,441
  • 3
  • 24
  • 36
  • Your question is not clear, what you exactly want to do please clear it. – Vickyexpert Nov 04 '16 at 04:29
  • @Nikita: Now it seems like you are stuck at the "item" array where there are multiple objects again. If that's the case then for that you can use getJSONarray and pass "item" to it and reiterate the object within the array, after you have known the count of array. – Max Nov 04 '16 at 06:07
  • Are you trying to create data-driven test? If it is the case you can use TestNG with [data-provider-extension](https://github.com/cbeust/testng/wiki/3rd-party-extensions#data-provider-extension). – user861594 Nov 04 '16 at 10:38

1 Answers1

1

It is very easy. First create a class lets say TestObject.java and Item.java as follows to suite your json strucuture.

TestObject.java

import java.util.List;

public class TestObject {
    private String name;
    private String type;
    private List<Item> items;
    private String successMsg;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the type
     */
    public String getType() {
        return type;
    }
    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }
    /**
     * @return the items
     */
    public List<Item> getItems() {
        return items;
    }
    /**
     * @param items the items to set
     */
    public void setItems(List<Item> items) {
        this.items = items;
    }
    /**
     * @return the successMsg
     */
    public String getSuccessMsg() {
        return successMsg;
    }
    /**
     * @param successMsg the successMsg to set
     */
    public void setSuccessMsg(String successMsg) {
        this.successMsg = successMsg;
    }
}

Item.java

public class Item {
    private String itemNo;
    private int qty;

    /**
     * @return the itemNo
     */
    public String getItemNo() {
        return itemNo;
    }
    /**
     * @param itemNo the itemNo to set
     */
    public void setItemNo(String itemNo) {
        this.itemNo = itemNo;
    }
    /**
     * @return the qty
     */
    public int getQty() {
        return qty;
    }
    /**
     * @param qty the qty to set
     */
    public void setQty(int qty) {
        this.qty = qty;
    }

}

Now parse your json string as follows. Below is the sample program which will help you understand the basics :

Test.java

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Test {

    public static void main(String[] args) {
        TestObject testObject = new TestObject();
        try {
            JSONObject jsonObject = new JSONObject("{\"name\": \"Test\","
                                                  + "\"type\": \"Private\","
                                                  + "\"item\":[{\"itmeNo\":\"PT-15003C\",\"quantity\":\"3\"},"
                                                  + "          {\"itmeNo\":\"PT-15003C\",\"quantity\":\"3\"}],"
                                                  + "\"successMsg\":\"Item(s) added to the job list.\"}"); //pass json string

            JSONArray jsonArray = jsonObject.getJSONArray("item"); //get the item jsonarray

            testObject.setName(jsonObject.getString("name"));
            testObject.setType(jsonObject.getString("type"));

            List<Item> items = new ArrayList<>();
            Item item = null;

            //Iterate the item array and add to the itemlist object
            for (int i = 0; i < jsonArray.length() ; i++) { 
                JSONObject jsonItem = jsonArray.getJSONObject(i);
                String itmeNo = jsonItem.getString("itmeNo");
                String quantity = jsonItem.getString("quantity");
                item = new Item();
                item.setItemNo(itmeNo);
                item.setQty(Integer.parseInt(quantity));
                items.add(item);
            } 

            testObject.setItems(items);
            testObject.setSuccessMsg(jsonObject.getString("successMsg")); //Now you will have a testObject which have all values from json.
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


}
Chirag Parmar
  • 833
  • 11
  • 26