1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.amber.oauth2.common.utils;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import org.codehaus.jettison.json.JSONException;
29 import org.codehaus.jettison.json.JSONObject;
30
31
32
33
34
35
36 public final class JSONUtils {
37
38 public static String buildJSON(Map<String, Object> params) throws JSONException {
39 JSONObject jsonObject = new JSONObject();
40 for (Map.Entry<String, Object> param : params.entrySet()) {
41 if (param.getKey() != null && !"".equals(param.getKey()) && param.getValue() != null && !""
42 .equals(param.getValue())) {
43 jsonObject.put(param.getKey(), param.getValue());
44 }
45 }
46
47 return jsonObject.toString();
48 }
49
50 public static Map<String, Object> parseJSON(String jsonBody) throws JSONException {
51
52 Map<String, Object> params = new HashMap<String, Object>();
53 JSONObject obj = new JSONObject(jsonBody);
54 Iterator it = obj.keys();
55 while (it.hasNext()) {
56 Object o = it.next();
57 if (o instanceof String) {
58 String key = (String)o;
59 params.put(key, obj.get(key));
60 }
61 }
62 return params;
63 }
64
65 }