I produce a json like this:
["\\"latitud\\":\\"123.0\\",\\"orden\\":\\"0\\",\\"longitud\\":\\"123.0\\",\\"urlfoto\\":\\"a\\",\\"idruta\\":\\"45\\"}","{\\"latitud\\":\\"321.0\\",\\"orden\\":\\"1\\",\\"longitud\\":\\"321.0\\",\\"urlfoto\\":\\"b\\",\\"idruta\\":\\"45\\"}","{\\"latitud\\":\\"231.0\\",\\"orden\\":\\"2\\",\\"longitud\\":\\"231.0\\",\\"urlfoto\\":\\"c\\",\\"idruta\\":\\"45\\"}"]
I search here and I have tryed:
$puntos = $_POST['puntos'];
$data = json_decode($puntos,true);
foreach($data as $obj) {
$idruta = $obj['idruta'];
$orden = $obj['orden'];
$urlfoto = $obj['urlfoto'];
$longitud = $obj['longitud'];
$latitud = $obj['latitud'];
}
Illegal string offset 'idruta'
foreach($data as $obj) {
$idruta = $obj->idruta;
$orden = $obj->orden;
$urlfoto = $obj->urlfoto;
$longitud = $obj->longitud;
$latitud = $obj->latitud;
}
Trying to get property of non-object
foreach($data as $obj) {
$idruta = $obj[0];
$orden = $obj[1];
$urlfoto = $obj[2];
$longitud = $obj[3];
$latitud = $obj[4];
}
obj[i] is always 0 and no errors.
The loop do 3 times so that's ok.
Sorry I'm just learning JSON and php, I will very glad if anybody can help me getting the data of the JSON.
Thanks!
EDIT: Thanks for the answers! I don't know why is missing the "{" and when i paste the same json in JSONlint for example its validates fine so... I'm a little lost sorry.
That's the way I am sending the json:
public void insertPoints(ArrayList<Punto> puntos){
JSONArray array = new JSONArray();
List<NameValuePair> params = new ArrayList<NameValuePair>();
for(Punto p:puntos){
JSONObject obj = new JSONObject();
try {
obj.put("idruta",Integer.toString(p.getIdruta()));
obj.put("orden",Integer.toString(p.getOrden()));
obj.put("urlfoto",p.getUrlfoto());
obj.put("longitud",Double.toString(p.getLongitud()));
obj.put("latitud",Double.toString(p.getLongitud()));
array.put(obj.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost(CREATE_POINT);
StringEntity params =new StringEntity("puntos=" + postjson);
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
// handle exception here
} finally {
httpClient.getConnectionManager().shutdown();
}
}
is here any problem?
Thanks!
First of all, {
is missing in first line of JSON.
Try this:
$data = json_decode($puntos,true);
instead of:
$data = json_decode($puntos);
It should work!
Your JSON represents an array of strings . All {
and }
are inside "..."
and are interpreted as part of a string. So, you can't access 'idruta'
and other fields without further parsing because they all are inside a single string. You should change JSON code if you can.
You problem is caused by array.put(obj.toString());
. You shouldn't do it. Also I think you should remove Integer.toString
from obj.put("idruta",Integer.toString(p.getIdruta()));
and similar lines. See this question .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.