JSONObject解析引发java.lang.OutOfMemoryError问题

前端之家收集整理的这篇文章主要介绍了JSONObject解析引发java.lang.OutOfMemoryError问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

先看代码


需要解析的json

{"code":0,"data":[{"id":null,"name":"Doe"},{"id":2,"name":"Sam"}]}


下面代码用JSONObject解析

	private void parseByJsonObject(String jsonStr) {
		try {
			JSONObject obj = new JSONObject(jsonStr);
			int code = obj.isNull("code") ? 0 : obj.getInt("code");
			JSONArray datas = obj.isNull("data") ? new JSONArray() : obj.getJSONArray("data");
			int length = datas.length();
			List<Person> persons = new ArrayList<Person>(length);
			for (int i = 0; i < length; i++) {
				JSONObject data = (JSONObject) datas.get(i);
				int id = data.isNull("id") ? 0 : data.getInt("id");
				String name = data.isNull("name") ? "" : data.getString("name");
				Person person = new Person();
				person.setId(id);
				person.setName(name);
				persons.add(person);
			}
			// 输出     code=0;persons=[{"id":0,"name":"Doe",},"name":"Sam",}]
			Log.e("scrat","code="+code + ";persons="+persons.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

这是一个比较常用的,把Json字符串转为JsonObject的方法


但是,如果这个jsonStr过大,会引发OOM异常


改用JsonReader来读取可以避免此异常 (时间关系,代码有点随意)

	@SuppressLint("NewApi")
	private void parseByJsonReader(String jsonStr) throws IOException {
		JsonReader jsonReader = null;
		InputStream in = null;
		Reader reader = null;
		try {
			in = new ByteArrayInputStream(jsonStr.getBytes());
			reader = new InputStreamReader(in,"UTF-8");
			jsonReader = new JsonReader(reader);
			
			int code = 0;
			List<Person> persons = new ArrayList<Person>();
			jsonReader.beginObject();
			while (jsonReader.hasNext()) {
				String tmpName1 = jsonReader.nextName();
				if ("code".equals(tmpName1)) {
					code = jsonReader.nextInt();
				} else if ("data".equals(tmpName1)) {
					jsonReader.beginArray();
					while (jsonReader.hasNext()) {
						int id = 0;
						String name = null;
						jsonReader.beginObject();
						while (jsonReader.hasNext()) {
							String tmpName2 = jsonReader.nextName(); // 一定要放到 jsonReader.peek()的判断前面,否则判断NULL无效
							if (jsonReader.peek() == JsonToken.NULL) {
								jsonReader.skipValue();
								continue;
							}
							if ("id".equals(tmpName2)) {
								id = jsonReader.nextInt();
							} else if ("name".equals(tmpName2)) {
								name = jsonReader.nextString();
							} else {
								jsonReader.skipValue();
							}
						}
						jsonReader.endObject();
						Person person = new Person();
						person.setId(id);
						person.setName(name);
						persons.add(person);
					}
					jsonReader.endArray();
				} else {
					jsonReader.skipValue();
				}
			}
			jsonReader.endObject();
			Log.e("scrat","code="+code + ";persons="+persons.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				reader.close();
			}
			if (in != null) {
				in.close();
			}
			if (jsonReader != null) {
				jsonReader.close();
			}
		}
	}
原文链接:https://www.f2er.com/json/290112.html

猜你在找的Json相关文章