-
import java.io.BufferedReader;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
import java.io.Reader;
-
import java.io.UnsupportedEncodingException;
-
import java.util.HashMap;
-
import java.util.Map;
-
import java.util.Scanner;
-
import com.google.gson.Gson;
-
import com.google.gson.GsonBuilder;
-
public class ConvertUtils {
-
// Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
-
static Gson gson = new Gson();
-
// JSON字符串转为Map数据类型
-
public static HashMap get_request_json_object(InputStream input_stream){
-
HashMap mapObj=null;
-
try {
-
BufferedReader streamReader = new BufferedReader(new InputStreamReader(input_stream,"UTF-8"));
-
StringBuilder responseStrBuilder = new StringBuilder();
-
String inputStr;
-
while ((inputStr = streamReader.readLine()) != null) responseStrBuilder.append(inputStr);
-
// mapObj = JSONObject.parseObject(responseStrBuilder.toString(),Map.class);
-
mapObj = gson.fromJson(responseStrBuilder.toString(), HashMap.class);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return mapObj;
-
}
-
// Map数据类型转为JSON字符串
-
public static String build_request_json_object(Map mapObj) {
-
String json_string = null;
-
try {
-
json_string = gson.toJson(mapObj);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return json_string;
-
}
-
}