要解析的字符串如下,是一串camera的参数:
{
"formats": [
{
"index": 1,
"type": 4,
"default": 1,
"size": [
"640*480",
"352*288",
"320*240",
"176*144",
"160*120"
]
}
]
}
ArrayList<PreviewSize> mPreviewSizes = new ArrayList<FormatParser.PreviewSize>();
private int index;
private int type;
private int def;
public FormatParser()
{
}
public void parseJsonFormat(String string)
{
try
{
JSONObject mJsonObject = new JSONObject(string);
Log.d(TAG, "JsonString:" + string);
JSONArray mArray = mJsonObject.getJSONArray("formats"); //获取formats数组
//formats中只有一个对象,直接从其中获取一些参数
index = mArray.getJSONObject(0).getInt("index");
type = mArray.getJSONObject(0).getInt("type");
def = mArray.getJSONObject(0).getInt("default");
//从formats中获取size数组
JSONArray mSizeArray = mArray.getJSONObject(0).getJSONArray("size");
mPreviewSizes.clear();
//遍历size数组,取出分辨率字符串,拆分成长和宽
for (int i = 0; i < mSizeArray.length(); i++)
{
PreviewSize mSize = new PreviewSize();
String strSize = mSizeArray.getString(i);
String[] strs = strSize.split("*");
mSize.width = Integer.parseInt(strs[0]);
mSize.height = Integer.parseInt(strs[1]);
Log.d(TAG, "width:" + mSize.width + " height:" + mSize.height);
}
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class PreviewSize
{
public int width;
public int height;
}