Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import javax.swing.text.MaskFormatter;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6.  
  7. public class MainTest {
  8.  
  9.  
  10. public static void main(String[] args){
  11.  
  12.  
  13. //makeJson();
  14. }
  15.  
  16. public static void makeJson(){
  17. String name = "prashun", age="25";
  18. String name2 = "palash", age2="26";
  19.  
  20. JSONObject obj = new JSONObject();
  21. try {
  22. obj.put("name",name);
  23. obj.put("age",age);
  24. } catch (JSONException e) {
  25. System.out.println(e.getMessage());
  26. }
  27.  
  28. JSONArray objA = new JSONArray();
  29. objA.put(obj);
  30.  
  31. obj = new JSONObject();
  32. try {
  33. obj.put("name",name2);
  34. obj.put("age",age2);
  35. } catch (JSONException e) {
  36. System.out.println(e.getMessage());
  37. }
  38.  
  39. objA.put(obj);
  40.  
  41. getJson(objA); // passing the JSONArray to another method
  42. }
  43.  
  44. public static void getJson(JSONArray objA){
  45. String name = "";
  46. String age = "";
  47. JSONObject obj = new JSONObject();
  48.  
  49. for(int i=0;i<objA.length();i++){
  50. try {
  51.  
  52. obj = objA.getJSONObject(i);
  53. name = obj.getString("name");
  54. age = obj.getString("age");
  55.  
  56. } catch (JSONException e) {
  57. System.out.println(e.getMessage());
  58. }
  59.  
  60. }
  61.  
  62. System.out.println(objA.toString());
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement