给定的数据最后不是有效的JSON垃圾 [英] The given data was not valid JSON garbage at end
问题描述
为什么会出现这个错误
MC]从公共有效用户设置中读取。错误:无法将数据解码到Blog:dataCorrupted(Swift.DecodingError.Context(codingPath:[],debugDescription:给定数据无效JSON。,underlyingError:可选(错误域= NSCocoaErrorDomain Code = 3840Garbage at at结束。UserInfo = {NSDebugDescription =结尾垃圾。})))
i有这个JSON
{响应:假,信息:[{Estatus:Sin Conexion}]}
这是我在swift中的json的结构
struct Blog:Decodable {
let Response:String
let articles:[article]
enum CodingKeys:String,CodingKey {
case response
case articles =Info
}
}
struct文章:可解码{
让Estatus:String
}
这是我的全部代码
让id1 = UserDefaults.standard.string(forKey:id1conductor)
let id1pa = UserDefaults.standard.string( forKey:idUser)
if adondevaxd.text ==|| adondevaxd.text ==|| adondevaxd.text ==123|| adondevaxd.text ==abc|| adondevaxd.text ==。
{
self.displayAlert(标题:Usuario,消息:Debe elegir un destino)
}
let myURL = URL( string:hehe)
var request = URLRequest(url:myURL!)
request.httpMethod =POST
let posString =ID_Conductor = \(id1!)& Destino = \(adondevaxd.text!)& ID_Pasajero = \(id1pa!)
request.httpBody = posString.data(using:.utf8)
let task = URLSession.shared.dataTask( with:request){
data,response,error in
if let error = error {
print(error = \(error))
return
}
guard let data = data else {
print(Something wrong)
return
}
** //这应该是一个有效的JSON,但不是我在另一个PHP中有相同的工作**
struct Blog:Decodable {
let Response:String
let用品:[文章]
enum CodingKeys:String,CodingKey {
case response
case articles =Info
}
}
struct Article:Decodable {
let Estatus:String
}
do {
let blog = try JSONDecoder() .decode(Blog.self,from:data)
DispatchQueue.main.async {
if blog.Response ==true{
// para ver como actua el json
// print(blog)
for blog.articles中的文章{
let tipoResponse = article.Estatus
如果tipoResponse ==Solicitado
{
self.displayAlert(标题:Usuario,消息:Elconductorseraànotificado,espera la respuesta)
self.buttonOu let.isHidden = true
self.adondevaxd.isHidden = true
self.adondevaxd.text =
self.adondequiereirxd.isHidden = true
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
self.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self。 Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
else tipoResponse ==Ocupado{
self.displayAlert(标题:Usuario,消息:El conductor ya se encuentra ocupado,selecciona otro)
self.map.isHidden = false
self.buttonOulet.isHidden = false
self.adondevaxd.isHidden = true
self.adondevaxd.text =
self.adondequiereirxd.isHidden = true
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
self.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self.Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
else如果tipoResponse ==Solicitado{
self.displayAl ert(标题:Usuario,消息:Se envio tu solicitud al conductor,porfavor ten paciencia)
self.adondequiereirxd.isHidden = true
self.adondevaxd.isHidden = true
self .adondequiereirxd.isHidden = true
}
break
}
}
else如果blog.Response ==false{
self.displayAlert(标题:Usuario,消息:No hay conexion)
self.map.isHidden = false
self.buttonOulet.isHidden = false
self.adondevaxd .isHidden = true
self.adondequiereirxd.isHidden = true
self.adondevaxd.text =
self.enviarDestino4.isHidden = true
self.enviarDestino3.isHidden = true
se lf.enviarDestino2.isHidden = true
self.enviarDestino1.isHidden = true
self.credencial1.isHidden = true
self.Credencial2.isHidden = true
self.Credencial3.isHidden = true
self.Credencial4.isHidden = true
}
}
}
catch {
print(错误:无法将数据解码到Blog中: ,错误)
返回
}
}
task.resume()
}
我的PHP错了吗?
任何提示? :S $ / $
<?php
require_once'../ data / d.php ;
error_reporting(0);
if(isset($ _ POST ['ID_Pasajero'])){
$ Comparacion =SELECT * FROM Solicitudes where ID_Conductor ='。$ _ POST ['ID_Conductor']。'和Estado!= '2';
$ R = $ conexion-> query($ Comparacion);
$ Count = mysqli_num_rows($ R);
if($ Count> 0){
$ Comparacion =SELECT * FROM Solicitudes where ID_Pasajero ='。$ _ POST ['ID_Pasajero']。'和Estado!='2';
$ R = $ conexion-> query($ Comparacion);
$ Count = mysqli_num_rows($ R);
if($ Count> 0){
$ Resultado ='{Response:true,Info:[{Estatus:Pendiente}}}';
} else {
$ Resultado ='{Response:true,Info:[{Estatus:Ocupado}]}';
}
} else {
$ Comparacion =SELECT * FROM Solicitudes,其中ID_Pasajero ='。$ _ POST ['ID_Pasajero']。'和Estado!='2';
$ R = $ conexion-> query($ Comparacion);
$ Count = mysqli_num_rows($ R);
if($ Count> 0){
$ Resultado ='{Response:true,Info:[{Estatus:Pendiente}}}';
} else {
$ sql =INSERT INTO Solicitudes(ID_Pasajero,ID_Conductor,Destino,Estado,Estado_Calificacion)VALUES('$ _POST [ID_Pasajero]','$ _POST [ID_Conductor]','$ _POST [Destino]','0','0');
$ Resultado ='{Response:true,Info:[{Estatus:Solicitado}}}';
}
}
}
if($ conexion-> query($ Comparacion)=== TRUE){
if($ conexion-> query( $ sql)=== TRUE){
echo $ Resultado;
} else {
echo $ Resultado;
}
} else {
if($ conexion-> query($ sql)=== TRUE){
echo $ Resultado;
}
echo'{Response:false,Info:[{Estatus:Sin Conexion}}}';
}
$ conexion-> close();
?>
我尝试过:
将所有代码更改为多种不同的方式
_POST ['ID_Pasajero'])){
< blockquote> Comparacion =SELECT * FROM Solicitudes where ID_Conductor ='。
_POST ['ID_Conductor']。'和Estado!='2';
why is this error popping out
MC] Reading from public effective user settings. Error: Couldn't decode data into Blog: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.})))
i have this JSON
{"Response":"false","Info":[{"Estatus":"Sin Conexion"}]}
this is the sctructure of my json in swift
struct Blog: Decodable { let Response: String let articles: [Article] enum CodingKeys : String, CodingKey { case Response case articles = "Info" } } struct Article: Decodable { let Estatus: String }
and this is all my code
let id1 = UserDefaults.standard.string(forKey: "id1conductor") let id1pa = UserDefaults.standard.string(forKey: "idUser") if adondevaxd.text == "" || adondevaxd.text == " " || adondevaxd.text == "123" || adondevaxd.text == "abc" || adondevaxd.text == "." { self.displayAlert(title: "Usuario", message: "Debe elegir un destino") } let myURL = URL(string: "hehe") var request = URLRequest(url: myURL!) request.httpMethod = "POST" let posString = "ID_Conductor=\(id1!)&Destino=\(adondevaxd.text!)&ID_Pasajero=\(id1pa!)" request.httpBody = posString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { data, response, error in if let error = error { print("error=\(error)") return } guard let data = data else { print("Something wrong") return } **// THIS SHOULD BE a valid JSON but is not i have the same in another PHP and it works** struct Blog: Decodable { let Response: String let articles: [Article] enum CodingKeys : String, CodingKey { case Response case articles = "Info" } } struct Article: Decodable { let Estatus: String } do { let blog = try JSONDecoder().decode(Blog.self, from: data) DispatchQueue.main.async { if blog.Response == "true" { //para ver como actua el json // print(blog) for article in blog.articles { let tipoResponse = article.Estatus if tipoResponse == "Solicitado" { self.displayAlert(title: "Usuario", message: "El conductor será notificado, espera la respuesta") self.buttonOulet.isHidden = true self.adondevaxd.isHidden = true self.adondevaxd.text = "" self.adondequiereirxd.isHidden = true self.enviarDestino4.isHidden = true self.enviarDestino3.isHidden = true self.enviarDestino2.isHidden = true self.enviarDestino1.isHidden = true self.credencial1.isHidden = true self.Credencial2.isHidden = true self.Credencial3.isHidden = true self.Credencial4.isHidden = true } else if tipoResponse == "Ocupado" { self.displayAlert(title: "Usuario", message: "El conductor ya se encuentra ocupado, selecciona otro") self.map.isHidden = false self.buttonOulet.isHidden = false self.adondevaxd.isHidden = true self.adondevaxd.text = "" self.adondequiereirxd.isHidden = true self.enviarDestino4.isHidden = true self.enviarDestino3.isHidden = true self.enviarDestino2.isHidden = true self.enviarDestino1.isHidden = true self.credencial1.isHidden = true self.Credencial2.isHidden = true self.Credencial3.isHidden = true self.Credencial4.isHidden = true } else if tipoResponse == "Solicitado" { self.displayAlert(title: "Usuario", message: "Se envio tu solicitud al conductor, porfavor ten paciencia") self.adondequiereirxd.isHidden = true self.adondevaxd.isHidden = true self.adondequiereirxd.isHidden = true } break } } else if blog.Response == "false" { self.displayAlert(title: "Usuario", message: "No hay conexion") self.map.isHidden = false self.buttonOulet.isHidden = false self.adondevaxd.isHidden = true self.adondequiereirxd.isHidden = true self.adondevaxd.text = "" self.enviarDestino4.isHidden = true self.enviarDestino3.isHidden = true self.enviarDestino2.isHidden = true self.enviarDestino1.isHidden = true self.credencial1.isHidden = true self.Credencial2.isHidden = true self.Credencial3.isHidden = true self.Credencial4.isHidden = true } } } catch { print("Error: Couldn't decode data into Blog:", error) return } } task.resume() }
is my PHP wrong?
any hints? :S
<?php require_once '../data/d.php'; error_reporting(0); if(isset($_POST['ID_Pasajero'])){ $Comparacion = "SELECT * FROM Solicitudes where ID_Conductor = '".$_POST['ID_Conductor']."' and Estado != '2'"; $R = $conexion->query($Comparacion); $Count = mysqli_num_rows($R); if ($Count > 0) { $Comparacion = "SELECT * FROM Solicitudes where ID_Pasajero = '".$_POST['ID_Pasajero']."' and Estado != '2'"; $R = $conexion->query($Comparacion); $Count = mysqli_num_rows($R); if ($Count > 0) { $Resultado = '{"Response":"true","Info":[{"Estatus":"Pendiente"}]}'; }else{ $Resultado = '{"Response":"true","Info":[{"Estatus":"Ocupado"}]}'; } }else{ $Comparacion = "SELECT * FROM Solicitudes where ID_Pasajero = '".$_POST['ID_Pasajero']."' and Estado != '2'"; $R = $conexion->query($Comparacion); $Count = mysqli_num_rows($R); if ($Count > 0) { $Resultado = '{"Response":"true","Info":[{"Estatus":"Pendiente"}]}'; }else{ $sql = "INSERT INTO Solicitudes(ID_Pasajero, ID_Conductor, Destino, Estado, Estado_Calificacion) VALUES ('$_POST[ID_Pasajero]', '$_POST[ID_Conductor]', '$_POST[Destino]', '0', '0')"; $Resultado = '{"Response":"true","Info":[{"Estatus":"Solicitado"}]}'; } } } if ($conexion->query($Comparacion) === TRUE) { if ($conexion->query($sql) === TRUE) { echo $Resultado; } else { echo $Resultado; } } else { if ($conexion->query($sql) === TRUE) { echo $Resultado; } echo '{"Response":"false","Info":[{"Estatus":"Sin Conexion"}]}'; } $conexion->close(); ?>
What I have tried:
changing all code to many different ways
_POST['ID_Pasajero'])){
Comparacion = "SELECT * FROM Solicitudes where ID_Conductor = '".
_POST['ID_Conductor']."' and Estado != '2'";
这篇关于给定的数据最后不是有效的JSON垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!