1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package com.ihealth.ihealthlibrary;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
/**
* Created by jing on 16/11/10.
*/
public class Utils {
private static final String TAG = "Utils";
public static void jsonToMap(String jsonString, WritableMap writableMap) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(jsonString);
if (jsonObject.length()>0) {
for (Iterator<String> keys=jsonObject.keys(); keys.hasNext();) {
String key = keys.next();
try {
Object object = jsonObject.get(key);
if (object instanceof JSONObject) {
writableMap.putMap(key, getMapFromJSONObject((JSONObject) object));
} else if (object instanceof JSONArray) {
writableMap.putArray(key, getWritableArrayFromJSONArray((JSONArray) object));
} else {
objectToMap(object, writableMap, key);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static WritableMap getMapFromJSONObject(JSONObject object) {
WritableMap map = Arguments.createMap();
Iterator<String> keyIterator = object.keys();
while (keyIterator.hasNext()) {
String key = keyIterator.next();
try {
Object value = object.get(key);
if (value instanceof JSONObject) {
// value is JSONObject case
map.putMap(key, getMapFromJSONObject((JSONObject) value));
} else if (value instanceof JSONArray) {
// value is JSONArray case
map.putArray(key, getWritableArrayFromJSONArray((JSONArray) value));
} else {
// Normal object case
objectToMap(value, map, key);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return map;
}
public static WritableArray getWritableArrayFromJSONArray(JSONArray array) {
WritableArray writableArray = Arguments.createArray();
for (int i = 0; i < array.length(); i++) {
try {
Object objectInArray = array.get(i);
if (objectInArray instanceof JSONObject) {
writableArray.pushMap(getMapFromJSONObject((JSONObject) objectInArray));
} else if (objectInArray instanceof Boolean) {
writableArray.pushBoolean((Boolean) objectInArray);
} else if(objectInArray instanceof Integer) {
writableArray.pushInt((Integer) objectInArray);
} else if(objectInArray instanceof Double) {
writableArray.pushDouble((Double) objectInArray);
} else if(objectInArray instanceof String) {
writableArray.pushString((String) objectInArray);
} else if(objectInArray instanceof Long) {
writableArray.pushDouble((Long) objectInArray);
} else {
Log.e(TAG, "Unknown type : " + objectInArray.getClass().getSimpleName());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return writableArray;
}
private static void objectToMap(Object object, WritableMap writableMap, String key) {
if (object instanceof Boolean) {
writableMap.putBoolean(key, (Boolean) object);
} else if(object instanceof Integer) {
writableMap.putInt(key, (Integer) object);
} else if(object instanceof Double) {
writableMap.putDouble(key, (Double) object);
} else if(object instanceof String) {
writableMap.putString(key, (String) object);
} else if(object instanceof Long) {
writableMap.putDouble(key, (Long) object);
} else {
Log.e(TAG, "Unknown type : " + object.getClass().getSimpleName());
}
}
}
|