从JSONObject

时间:2017-03-09 05:41:47

标签: java json

我正在尝试从我的JSONObject中提取Key和值。但我无法做到这一点。这是JSON:

[{"id":["5"]},{"Tech":["Java"]}]

最初是一个字符串。我已使用:

将其转换为JSONObject
JSONObject jsonObj = new JSONObject("[{"id":["5"]},{"Tech":["Java"]}]");

然后我试图通过以下方式获得关键和价值:

jsonObj.getString("id");

但是它让我无效。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:7)

试试这个: 尝试{     JSONArray jsonArr = new JSONArray(“[{\”id \“:[\”5 \“]},{\”Tech \“:[\”Java \“]}]”);     for(int i = 0; i< jsonArr.length(); i ++){         JSONObject jsonObj = jsonArr.getJSONObject(i);         String k = jsonObj.keys()。next();         Log.i(“Info”,“Key:”+ k +“,value:”+ jsonObj.getString(k));     } } catch(JSONException ex){     ex.printStackTrace(); }

答案 1 :(得分:1)

您发送的参数是JsonArray并引用JsonObject。正确的方法是

JSONObject jsonObj = new JSONObject("{'id':['5','6']},{'Tech':['Java']}");      
    System.out.println(jsonObj.getString("id"));

    JSONArray jsonArray = new JSONArray("[{'id':['5','6','7','8']},{'Tech':['Java']}]");
    System.out.println(jsonArray.length());
    for(int i=0;i<jsonArray.length();i++){
            System.out.println(jsonArray.getJSONObject(i).getString("id"));
    }

答案 2 :(得分:-1)

因为在Use Alamofire for image upload - func makePostRequestForUploadingImage(_ service : String!, imageData : Data?, responseCallBack : APIClientResponseCallBack!) { let urlString = self.urlStringWithService(service) Alamofire.upload(multipartFormData: { (multipartFormData) in if let _ = imageData { multipartFormData.append(imageData!, withName: "", fileName: "", mimeType: "image/jpg") } }, to: urlString) { (encodingresult) in switch encodingresult { case .success(let upload, _, _): upload.responseJSON { response in responseCallBack? (response.response?.statusCode,response.result.value as AnyObject? , response.result.error as NSError?) } case .failure(let encodingError): print(encodingError) } } } static func createBodyForMultiPart(input:Any, previousString:String,multipartFormData: MultipartFormData) { print("previousString -- ", previousString) if let dic = input as? [String:Any] { for key in dic.keys { self.createBodyForMultiPart(input: dic[key]!, previousString: previousString != "" ? (previousString + "[\(key)]") : "\(key)",multipartFormData: multipartFormData) } } else if let array = input as? [Any] { for i in 0..<array.count{ self.createBodyForMultiPart(input: array[i], previousString: previousString + "[\(i)]",multipartFormData: multipartFormData) } } else if let number = input as? Int { multipartFormData.append(String(number).data(using:String.Encoding.utf8,allowLossyConversion:false )!, withName: previousString) } else if let text = input as? String{ multipartFormData.append(text.data(using:String.Encoding.utf8,allowLossyConversion :false)!, withName: previousString) } else if let image = input as? UIImage { multipartFormData.append(UIImageJPEGRepresentation(image, 0.2)!, withName: previousString, fileName: "inspectionitem.jpeg", mimeType: "image/jpg") } else if let floatNumber = input as? Float { multipartFormData.append(String(floatNumber).data(using:String.Encoding.utf8,allowLossyConversion :false)!, withName: previousString) } } 你没有字符串,所以你有一个字符串数组

所以这样做了 id

这样做

jsonObj.getString("id");这会给你那个数组作为回报

就像你在jsonObj.getArray("id");有一个Json数组一样,你必须这样做

id