[英]I can't read data from JSON
I produce a json like this: 我产生这样的json:
["\\"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\\"}"]
[“ \\” latitud \\“:\\” 123.0 \\“,\\” orden \\“:\\” 0 \\“,\\” longitud \\“:\\” 123.0 \\“,\\” urlfoto \\“:\\” a \\“, \\“ idruta \\”:\\“ 45 \\”}“,” {\\“ latitud \\”:\\“ 321.0 \\”,\\“ orden \\”:\\“ 1 \\”,\\“纵向\\”:\\“ 321.0 \\ “,\\” urlfoto \\“:\\” b \\“,\\” idruta \\“:\\” 45 \\“}”,“ {\\” latitud \\“:\\” 231.0 \\“,\\” orden \\“:\\” 2 \\“,\\”纵向\\“:\\” 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' 非法字符串偏移量'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. obj [i]始终为0,没有错误。
The loop do 3 times so that's ok. 循环执行3次,这样就可以了。
Sorry I'm just learning JSON and php, I will very glad if anybody can help me getting the data of the JSON. 抱歉,我只是在学习JSON和php,如果有人可以帮助我获取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.
我不知道为什么会丢失“ {”,例如,当我在JSONlint中粘贴相同的json时,它可以很好地进行验证,所以...我有点遗憾。
That's the way I am sending the json: 这就是我发送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. 首先,JSON第一行中缺少
{
。
Try this: 尝试这个:
$data = json_decode($puntos,true);
instead of: 代替:
$data = json_decode($puntos);
It should work! 它应该工作!
Your JSON represents an array of strings . 您的JSON表示一个字符串数组。 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. 因此,您不能在不进行进一步解析的情况下访问
'idruta'
和其他字段,因为它们都在单个字符串中。 You should change JSON code if you can. 如果可以,您应该更改JSON代码。
You problem is caused by array.put(obj.toString());
您的问题是由
array.put(obj.toString());
引起的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()));
我也认为您应该从
obj.put("idruta",Integer.toString(p.getIdruta()));
删除Integer.toString
obj.put("idruta",Integer.toString(p.getIdruta()));
and similar lines. 和类似的线条。 See this question .
看到这个问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.