我知道Stackoverflow上有类似的问题,但我没有找到任何帮助解决我遇到的问题。
我有这个JSON:
[{
"name": "Name0",
"services": [
[{
"Service": "Service00",
"Description": "Desc00"
}, {
"Service": "Service01",
"Description": "Desc01"
}]
]
}, {
"name": "Name1",
"services": [
[{
"Service": "Service10",
"Description": "Desc10"
}]
]
}]
我用:
循环$quoteJson = json_decode($quoteJson);
foreach($quoteJson as $mydata) {
echo $mydata->name . "<br>";
foreach($mydata->services as $key => $value)
{
echo $value[$key]->Service . "<br>";
echo $value[$key]->Description . "<br>";
}
}
我得到的结果是:
Name0
Service00
Desc00
Name1
Service10
Desc10
我无法遍历服务元素,获取:
Name0
Service00
Desc00
Service01
Desc01
Name1
Service10
Desc10
答案 0 :(得分:3)
由于某种原因,import UIKit
import Vision
import MobileCoreServices
import AVFoundation
import Photos
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {
var newMedia: Bool?
@IBAction func captureImageButtonPressed(_ sender: Any) {
//let imageName : String = "dolphin"
//randomImageView.image = UIImage.init(named:imageName)
if UIImagePickerController.isSourceTypeAvailable(
UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true,
completion: nil)
newMedia = true
}
}
@IBAction func classifyButtonPressed(_ sender: UIButton) {
performVisionRequest()
}
@IBOutlet weak var randomImageView: UIImageView!
@IBOutlet weak var classificationLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
func performVisionRequest() {
let start = DispatchTime.now()
let model = Resnet50()
let request = VNImageRequestHandler(cgImage: randomImageView.image!.cgImage!, options: [:])
do {
let m = try VNCoreMLModel(for: model.model)
let coreMLRequest = VNCoreMLRequest(model: m) { (request, error) in
guard let observation = request.results?.first as? VNClassificationObservation else { return }
let stop = DispatchTime.now()
let nanoTime = stop.uptimeNanoseconds - start.uptimeNanoseconds
let timeInterval = Double(nanoTime)
self.classificationLabel.text = "\(observation.identifier) (\(observation.confidence * 100)%) in \(timeInterval) seconds."
}
try request.perform([coreMLRequest])
} catch {
print(error)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismiss(animated: true, completion: nil)
if mediaType.isEqual(to: kUTTypeImage as String) {
let image = info[UIImagePickerControllerOriginalImage]
as! UIImage
randomImageView.image = image
if (newMedia == true) {
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
} else if mediaType.isEqual(to: kUTTypeMovie as String) {
// Code to support video here
}
}
}
@objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
if error != nil {
let alert = UIAlertController(title: "Save Failed",
message: "Failed to save image",
preferredStyle: UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK",
style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true,
completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
是数组中的数组。对您的代码进行了一些小改动:
services
现在又回来了:
NAME0
Service00
Desc00
Service01
Desc01
名1
Service10
Desc10
答案 1 :(得分:1)
由于$ mydata-&gt; services是多维数组,因此需要循环$ value变量。
$quoteJson = json_decode($quoteJson);
foreach($quoteJson as $mydata) {
echo $mydata->name . "\n";
foreach($mydata->services as $key => $value)
{
foreach($value as $k=>$v){ // loop the array
echo $v->Service . "\n";
echo $v->Description . "\n";
}
}
}
答案 2 :(得分:1)
输出不符合预期,因为您错过了内循环。 下面的代码工作正常。
foreach($quoteJson as $mydata) {
echo $mydata->name . "<br>";
foreach($mydata->services as $key => $value)
{
foreach($value as $innerdata){
echo $innerdata->Service . "<br>";
echo $innerdata->Description . "<br>";
}
}
}