rstList) {
key = BBSUtils.replaceBlank(key);
if (value instanceof String) {
rstList.put(key, BBSUtils.replaceBlank((String) value));
return;
rstList.put(key, value);
public static String getRaw(Context context, int RawId) {
try {
InputStream is = context.getResources().openRawResource(RawId);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
// StringBuffer线程安全;StringBuilder线程不安全
StringBuffer sb = new StringBuffer();
for (String str = null; (str = reader.readLine()) != null;) {
sb.append(str);
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
public static String getAsset(Context context, String fileName) {
try {
InputStream is = context.getResources().getAssets().open(fileName);
// StringBuffer线程安全;StringBuilder线程不安全
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
for (String str = reader.readLine(); str != null;) {
sb.append(str);
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
JsonObject里面有两种情况:(1)JsonObject (2)JsonArray
JsonArray 后面有三种情况:(1)JsonObject (2)JsonArray (3)Object
====================================================
2.调用类:
String jsonStr = Utils.getRaw(mContext, R.raw.file1);
String temp = "{\"\":[{\"aa\":\"1\",\"bb\":\"2\"},{\"aa\":\"3\",\"bb\":\"4\"},{\"aa\":\"5\",\"bb\":\"6\"}]}";
System.out.println("---------jsonStr" + jsonStr);
ArrayList<Map<?, ?>> rstList = new ArrayList<Map<?, ?>>();
try {
Utils.JsonObject2HashMap(new JSONObject(jsonStr), rstList);
Utils.JsonObject2HashMap(new JSONObject(temp), rstList);
System.out.println("---------rstList" + rstList);
} catch (JSONException e) {
e.printStackTrace();
3.Raw文件夹下文本文件file1:
"programmers": [
"firstName": "Brett",
"lastName": "McLaughlin"
"firstName": "Jason",
"lastName": "Hunter"
"authors": [
"firstName": "Isaac",
"lastName": "Asimov"
"firstName": "Tad",
"lastName": "Williams"
=================================================================
import java.util.Map.Entry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
JSONObject user = resJson.getJSONObj
HashMap<Long,String> hashMap = new HashMap<>();
hashMap.put(1000000000001L,"一");
hashMap.put(1000000000002L,"二");
hashMap.put(1000000000003L,"三");
JSONObject jsonObject = new JSONObject();
jsonObject.put(...
ServletInputStream json = request.getInputStream();
HashMap<String,String> map = new ObjectMapper().readValue(json, HashMap.class);
以上是我在和前端交互时使用的,可以行的通!
public static void main(String[] args){
String str = "{"0":"zhangsan","1":"lisi","2":"wangwu","3":"maliu"}";
//第一种方式
Map ma
如何把JSON对象转为map对象呢?JSON 对象保存在大括号内。就像在JavaScript中, 对象可以保存多个 键/值 对。Map对象保存键/值对,是键/值对的集合。任何值(对象或者原始值) 都可以作为一个键或一个值。Object结构提供了“字符串—值”的对应,Map结构提供了“值—值”的对应。javascript将JSON对象转为map对象可以利用阿里巴巴封装的FastJSON来转换。有多种...
使用json格式的数据需要的jar包,除了本身的jar包,还有一些需要依赖的jar包
总结一下包括:
要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:
commons-lang-2.4.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.jar
commons-logging1.1.1.jar
ezmorph-1.0.4.jar
json-lib-2.4-jdk15.jar
1.工具类:Utils.class:(1)简单的键值对mappublicclassUtils{publicstaticStringgetRaw(Contextcontext,intRawId){try{InputStreamis=context.getResources().openRawResource(RawId);BufferedReaderreader=n...
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {
// do something
public class JSONObject extends J