假设我们有下一个JSON字符串:
{
"name" : "John",
"age" : "20",
"address" : "some address",
"someobject" : {
"field" : "value"
}
}
Run Code Online (Sandbox Code Playgroud)
什么是最简单的(但仍然是正确的,即正则表达式是不可接受的)查找字段age及其值的方法(或确定没有给定名称的字段)?
ps任何开源库都可以.
ps2:拜托,不要发布图书馆的链接 - 这不是一个有用的答案.'给我看代码'(c).
我创建了UploadToImgurTask一个AsyncTask 类,它接受单个文件路径参数,创建并设置MultiPartEntity,然后使用Apache HttpClient上传带有所述实体的图像.来自Imgur的JSON响应保存在JSONObject中,我在LogCat中显示的内容供我自己理解.
这是我从Imgur收到的JSON的屏幕截图:
我在api.imgur.com上查找了错误状态401,它说我需要使用OAuth进行身份验证,尽管事实上Imgur已经明确表示如果图像是匿名上传的,应用程序不需要使用OAuth(这就是我我现在正在做
class UploadToImgurTask extends AsyncTask<String, Void, Boolean> {
String upload_to;
@Override
protected Boolean doInBackground(String... params) {
final String upload_to = "https://api.imgur.com/3/upload.json";
final String API_key = "API_KEY";
final String TAG = "Awais";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(upload_to);
try {
final MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("image", new FileBody(new File(params[0])));
entity.addPart("key", new StringBody(API_key));
httpPost.setEntity(entity);
final HttpResponse response = httpClient.execute(httpPost,
localContext);
final String response_string …Run Code Online (Sandbox Code Playgroud)