Android gson解析

本文介绍了一个使用Java处理JSON数据的方法,包括从数据库中获取支柱状态和缺陷信息,并将其转化为JSON格式,同时展示了如何使用Gson库将JSON字符串解析成JavaBean对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.epapandroid.comm.CommonUtil;

import android.R.bool;
import android.R.integer;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.util.Base64;

public JSONArray GetUploadTaskInfo(Integer DAYRANGEID) {
		JSONArray jsonArray = new JSONArray();

		DayPlanDevStatueService dayPlanDevStatueService = new DayPlanDevStatueService(
				m_context);
		DefectInfoService defectInfoService = new DefectInfoService(m_context);
		DefectPhotoService defectPhotoService = new DefectPhotoService(
				m_context);
		// 获取支柱状态。
		Cursor cursor = dayPlanDevStatueService
				.getCursorDevStatueByUp(DAYRANGEID);

		try {

			while (cursor.moveToNext()) {
				JSONObject jsonObject = new JSONObject();
				Object dObject = cursor.getString(cursor
						.getColumnIndex("STATUE"));
				Integer DEVICEID = cursor.getInt(cursor
						.getColumnIndex("DEVICEID"));// 支柱ID。
				jsonObject.put(
						"DAYPLANDEVSTATUEID",
						cursor.getInt(cursor
								.getColumnIndex("DAYPLANDEVSTATUEID"))).put(
						"STATUE", dObject);

				// 获取缺陷信息
				Cursor cursorDefect = defectInfoService
						.getCursorDefectInfoByRangID(DAYRANGEID, DEVICEID);
				// 获取缺陷状态。
				Cursor cursorDefectPhoto = defectPhotoService
						.getCursorDefectPhoto(DAYRANGEID, DEVICEID);
				JSONArray jsonArrayDefect = new JSONArray();
				while (cursorDefect.moveToNext()) {
					/**
					 * 构造JSON缺陷记录
					 */
					JSONObject jsonObjectDefect = new JSONObject();
					jsonObjectDefect.put("DEFECTINFOID",
							cursorDefect.getInt(cursorDefect
									.getColumnIndex("DEFECTINFOID")));
					jsonObjectDefect.put("DAYRANGEID", cursorDefect
							.getInt(cursorDefect.getColumnIndex("DAYRANGEID")));
					jsonObjectDefect.put("SITEID", cursorDefect
							.getInt(cursorDefect.getColumnIndex("SITEID")));
					jsonObjectDefect.put("PDEVICEID", cursorDefect
							.getInt(cursorDefect.getColumnIndex("PDEVICEID")));
					jsonObjectDefect.put("DEVICEID", cursorDefect
							.getInt(cursorDefect.getColumnIndex("DEVICEID")));
					jsonObjectDefect.put("DEFECTTYPEID_LARGE", cursorDefect
							.getInt(cursorDefect
									.getColumnIndex("DEFECTTYPEID_LARGE")));
					jsonObjectDefect.put("DEFECTTYPEID_MIDDLE", cursorDefect
							.getInt(cursorDefect
									.getColumnIndex("DEFECTTYPEID_MIDDLE")));
					jsonObjectDefect.put("DEFECTTYPEID_SMALL", cursorDefect
							.getInt(cursorDefect
									.getColumnIndex("DEFECTTYPEID_SMALL")));
					jsonObjectDefect.put("DESCR", cursorDefect
							.getString(cursorDefect.getColumnIndex("DESCR")));
					jsonObjectDefect.put("DEFECTREASONID_LARGE", cursorDefect
							.getInt(cursorDefect
									.getColumnIndex("DEFECTREASONID_LARGE")));
					jsonObjectDefect.put("DEFECTREASONID_SMALL", cursorDefect
							.getInt(cursorDefect
									.getColumnIndex("DEFECTREASONID_SMALL")));
					jsonObjectDefect.put("LONGITUDE", cursorDefect
							.getLong(cursorDefect.getColumnIndex("LONGITUDE")));
					jsonObjectDefect.put("LATITUDE", cursorDefect
							.getLong(cursorDefect.getColumnIndex("LATITUDE")));
					jsonObjectDefect.put("CREATEDATE", cursorDefect
							.getString(cursorDefect
									.getColumnIndex("CREATEDATE")));
					jsonObjectDefect.put("STATUE", cursorDefect
							.getInt(cursorDefect.getColumnIndex("STATUE")));
					jsonObjectDefect.put("SOLVEMETHOD", cursorDefect
							.getString(cursorDefect
									.getColumnIndex("SOLVEMETHOD")));
					JSONArray jsonArrayDefectPhoto = new JSONArray();
					/***
					 * 构造缺陷图片JSON
					 */
					while (cursorDefectPhoto.moveToNext()) {
						JSONObject jsonObjectDefectPhoto = new JSONObject();
						jsonObjectDefectPhoto.put("FILENAME", cursorDefectPhoto
								.getString(cursorDefectPhoto
										.getColumnIndex("FILENAME")));
						String FILEPATH = cursorDefectPhoto
								.getString(cursorDefectPhoto
										.getColumnIndex("FILEPATH"));
						// 将图片转为数据流。
						FileInputStream fis;
						String uploadBuffer = "";
						try {
							fis = new FileInputStream(FILEPATH);

							ByteArrayOutputStream baos = new ByteArrayOutputStream();
							byte[] buffer = new byte[1024];
							int count = 0;
							while ((count = fis.read(buffer)) >= 0) {
								baos.write(buffer, 0, count);
							}
							uploadBuffer = new String(Base64.encode(
									baos.toByteArray(), Base64.DEFAULT)); // 进行Base64编码
						} catch (FileNotFoundException e) {

							e.printStackTrace();
						} catch (IOException e) {

							e.printStackTrace();
						}
						jsonObjectDefectPhoto.put("FILEBUFFER", uploadBuffer);
						jsonArrayDefectPhoto.put(jsonObjectDefectPhoto);
					}
					jsonObjectDefect.put("DEFECTPHOTO", jsonArrayDefectPhoto);

					jsonArrayDefect.put(jsonObjectDefect);
				}

				jsonArray.put(jsonObject);
			}
		} catch (JSONException e) {

			e.printStackTrace();
		}
		return jsonArray;
	}

将json字符串解析成一个javaBean:

public class MainActivity extends Activity {
	private Button startButton = null;
	private String data = "{\"name\":\"zhangsan\",\"age\":20}";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		startButton = (Button)findViewById(R.id.button1);
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Gson gson = new Gson();
				
				User user = gson.fromJson(data, User.class);
				System.out.println("name---->" + user.getName());
				System.out.println("age----->" + user.getAge());
			}
		});
	}


}

将json字符串解析成多个JavaBean对象:

public class MainActivity_2 extends Activity {
	private Button startButton = null;
	private String data = "[{\"name\":\"zhangsan\",\"age\":20},{\"name\":\"lisi\",\"age\":21}]";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startButton = (Button)findViewById(R.id.button1);
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Type type = new TypeToken<ArrayList<User>>(){}.getType();
				Gson gson = new Gson();
				ArrayList<User> list = gson.fromJson(data, type);
				Iterator<User> iterator = list.iterator();
				while (iterator.hasNext()) {
					User user = iterator.next();
					System.out.println("name--->" + user.getName());
					System.out.println("age---->" + user.getAge());
				}
			}
		});
	}

}

public class User {
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值