Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.59 KB | None | 0 0
  1. [{"thumb_url":"tb-1370913834.jpg","event_id":"15","count":"44","event_tagline":"this is a tagline","event_name":"5th birthday","event_end":"1370919600","event_start":"1370876400"}]
  2.  
  3. ArrayList<String> listdata = new ArrayList<String>();
  4. JSONArray jArray = (JSONArray)jsonObject;
  5. if (jArray != null) {
  6. for (int i=0;i<jArray.length();i++){
  7. listdata.add(jArray.getString(i));
  8. }
  9. }
  10.  
  11. private String jsonString = "[n" +
  12. " {n" +
  13. " "id": "c200",n" +
  14. " "name": "Ravi Tamada",n" +
  15. " "email": "ravi@gmail.com",n" +
  16. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  17. " "gender" : "male",n" +
  18. " "phone": {n" +
  19. " "mobile": "+91 0000000000",n" +
  20. " "home": "00 000000",n" +
  21. " "office": "00 000000"n" +
  22. " }n" +
  23. " },n" +
  24. " {n" +
  25. " "id": "c201",n" +
  26. " "name": "Johnny Depp",n" +
  27. " "email": "johnny_depp@gmail.com",n" +
  28. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  29. " "gender" : "male",n" +
  30. " "phone": {n" +
  31. " "mobile": "+91 0000000000",n" +
  32. " "home": "00 000000",n" +
  33. " "office": "00 000000"n" +
  34. " }n" +
  35. " },n" +
  36. " {n" +
  37. " "id": "c202",n" +
  38. " "name": "Leonardo Dicaprio",n" +
  39. " "email": "leonardo_dicaprio@gmail.com",n" +
  40. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  41. " "gender" : "male",n" +
  42. " "phone": {n" +
  43. " "mobile": "+91 0000000000",n" +
  44. " "home": "00 000000",n" +
  45. " "office": "00 000000"n" +
  46. " }n" +
  47. " },n" +
  48. " {n" +
  49. " "id": "c203",n" +
  50. " "name": "John Wayne",n" +
  51. " "email": "john_wayne@gmail.com",n" +
  52. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  53. " "gender" : "male",n" +
  54. " "phone": {n" +
  55. " "mobile": "+91 0000000000",n" +
  56. " "home": "00 000000",n" +
  57. " "office": "00 000000"n" +
  58. " }n" +
  59. " },n" +
  60. " {n" +
  61. " "id": "c204",n" +
  62. " "name": "Angelina Jolie",n" +
  63. " "email": "angelina_jolie@gmail.com",n" +
  64. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  65. " "gender" : "female",n" +
  66. " "phone": {n" +
  67. " "mobile": "+91 0000000000",n" +
  68. " "home": "00 000000",n" +
  69. " "office": "00 000000"n" +
  70. " }n" +
  71. " },n" +
  72. " {n" +
  73. " "id": "c205",n" +
  74. " "name": "Dido",n" +
  75. " "email": "dido@gmail.com",n" +
  76. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  77. " "gender" : "female",n" +
  78. " "phone": {n" +
  79. " "mobile": "+91 0000000000",n" +
  80. " "home": "00 000000",n" +
  81. " "office": "00 000000"n" +
  82. " }n" +
  83. " },n" +
  84. " {n" +
  85. " "id": "c206",n" +
  86. " "name": "Adele",n" +
  87. " "email": "adele@gmail.com",n" +
  88. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  89. " "gender" : "female",n" +
  90. " "phone": {n" +
  91. " "mobile": "+91 0000000000",n" +
  92. " "home": "00 000000",n" +
  93. " "office": "00 000000"n" +
  94. " }n" +
  95. " },n" +
  96. " {n" +
  97. " "id": "c207",n" +
  98. " "name": "Hugh Jackman",n" +
  99. " "email": "hugh_jackman@gmail.com",n" +
  100. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  101. " "gender" : "male",n" +
  102. " "phone": {n" +
  103. " "mobile": "+91 0000000000",n" +
  104. " "home": "00 000000",n" +
  105. " "office": "00 000000"n" +
  106. " }n" +
  107. " },n" +
  108. " {n" +
  109. " "id": "c208",n" +
  110. " "name": "Will Smith",n" +
  111. " "email": "will_smith@gmail.com",n" +
  112. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  113. " "gender" : "male",n" +
  114. " "phone": {n" +
  115. " "mobile": "+91 0000000000",n" +
  116. " "home": "00 000000",n" +
  117. " "office": "00 000000"n" +
  118. " }n" +
  119. " },n" +
  120. " {n" +
  121. " "id": "c209",n" +
  122. " "name": "Clint Eastwood",n" +
  123. " "email": "clint_eastwood@gmail.com",n" +
  124. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  125. " "gender" : "male",n" +
  126. " "phone": {n" +
  127. " "mobile": "+91 0000000000",n" +
  128. " "home": "00 000000",n" +
  129. " "office": "00 000000"n" +
  130. " }n" +
  131. " },n" +
  132. " {n" +
  133. " "id": "c2010",n" +
  134. " "name": "Barack Obama",n" +
  135. " "email": "barack_obama@gmail.com",n" +
  136. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  137. " "gender" : "male",n" +
  138. " "phone": {n" +
  139. " "mobile": "+91 0000000000",n" +
  140. " "home": "00 000000",n" +
  141. " "office": "00 000000"n" +
  142. " }n" +
  143. " },n" +
  144. " {n" +
  145. " "id": "c2011",n" +
  146. " "name": "Kate Winslet",n" +
  147. " "email": "kate_winslet@gmail.com",n" +
  148. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  149. " "gender" : "female",n" +
  150. " "phone": {n" +
  151. " "mobile": "+91 0000000000",n" +
  152. " "home": "00 000000",n" +
  153. " "office": "00 000000"n" +
  154. " }n" +
  155. " },n" +
  156. " {n" +
  157. " "id": "c2012",n" +
  158. " "name": "Eminem",n" +
  159. " "email": "eminem@gmail.com",n" +
  160. " "address": "xx-xx-xxxx,x - street, x - country",n" +
  161. " "gender" : "male",n" +
  162. " "phone": {n" +
  163. " "mobile": "+91 0000000000",n" +
  164. " "home": "00 000000",n" +
  165. " "office": "00 000000"n" +
  166. " }n" +
  167. " }n" +
  168. " ]";
  169.  
  170. public class ContactModel{
  171. public String id;
  172. public String name;
  173. public String email;
  174. }
  175.  
  176. Gson gson = new Gson();
  177. Type type = new TypeToken<List<ContactModel>>(){}.getType();
  178. List<ContactModel> contactList = gson.fromJson(jsonString, type);
  179. for (ContactModel contact : contactList){
  180. Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
  181. }
  182.  
  183. JSONObject json = new JSONObject(jsonString);
  184.  
  185. JSONArray jsonArray = new JSONArray(jsonArrayString);
  186. List<String> list = new ArrayList<String>();
  187. for (int i=0; i<jsonArray.length(); i++) {
  188. list.add( jsonArray.getString(i) );
  189.  
  190. import java.util.ArrayList;
  191. import java.util.Collection;
  192. import org.json.JSONArray;
  193. import org.json.JSONException;
  194.  
  195. public class ArrayUtil
  196. {
  197. public static ArrayList<Object> convert(JSONArray jArr)
  198. {
  199. ArrayList<Object> list = new ArrayList<Object>();
  200. try {
  201. for (int i=0, l=jArr.length(); i<l; i++){
  202. list.add(jArr.get(i));
  203. }
  204. } catch (JSONException e) {}
  205.  
  206. return list;
  207. }
  208.  
  209. public static JSONArray convert(Collection<Object> list)
  210. {
  211. return new JSONArray(list);
  212. }
  213.  
  214. }
  215.  
  216. ArrayList<Object> list = ArrayUtil.convert(jArray);
  217.  
  218. JSONArray jArr = ArrayUtil.convert(list);
  219.  
  220. JSONArray array = new JSONArray(json);
  221. List<JSONObject> list = new ArrayList();
  222. for (int i = 0; i < array.length();list.add(array.getJSONObject(i++)));
  223.  
  224. List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>();
  225.  
  226. JSONObject jsonObject = new JSONObject(jsonString);
  227. YourPojoObject yourPojo = new YourPojoObject();
  228. yourPojo.setId(jsonObject.getString("idName"));
  229. ...
  230. ...
  231.  
  232. yourPojos.add(yourPojo);
  233.  
  234. public static List<JSONObject> getJSONObjectListFromJSONArray(JSONArray array)
  235. throws JSONException {
  236. ArrayList<JSONObject> jsonObjects = new ArrayList<>();
  237. for (int i = 0;
  238. i < (array != null ? array.length() : 0);
  239. jsonObjects.add(array.getJSONObject(i++))
  240. );
  241. return jsonObjects;
  242. }
  243.  
  244. IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList())
  245.  
  246. ArrayList<String> dataList = new ArrayList<String>();
  247. JSONArray jsonArray = (JSONArray)jsonObject;
  248. for(Object obj : jsonArray){
  249. dataList.add((String)obj);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement