报错 net.sf.json.JSONException

net.sf.json.JSONException: A JSONArray text must start with ‘[’ at character 1 of {“aaa”:“111”,“bbb”:“222”}

传过来的json 为 {“aaa”:“111”,“bbb”:“222”}

需要改为 [ {“aaa”:“111”,“bbb”:“222”} ]

原代码

		if (result != null && !"".equals(result)) {
			JSONArray jsonarray = JSONArray.fromObject(result);//此处报错
			String jsonString = jsonarray.getString(0);
			Map deviceMap = new HashMap<>();
			Gson gson = new Gson();
			deviceMap = gson.fromJson(jsonString, deviceMap.getClass());
			System.out.println(deviceMap.get("aaa"));
		}

修改后

		if (result != null && !"".equals(result)) {
			**result = "["+result+"]";**
			JSONArray jsonarray = JSONArray.fromObject(result);
			String jsonString = jsonarray.getString(0);
			Map deviceMap = new HashMap<>();
			Gson gson = new Gson();
			deviceMap = gson.fromJson(jsonString, deviceMap.getClass());
			System.out.println(deviceMap.get("aaa"));
		}

你可能感兴趣的:(报错 net.sf.json.JSONException)