dtye7921 2011-06-11 16:34
浏览 19
已采纳

json混乱

I have created and Android app that has to communicate with my website using JSON. JSON (on client, Android side) looks like this:

private static String JSONSend(Context ctx, JSONObject obj, String ObjName, String address) {
    IHttpDispatcher disp = new HttpDispatcher();
    Vector<String> ss = new Vector<String>();
    String link = address;

    String locale = uzmiLocale(ctx);
    if(locale=="")
        return "";

    try {
        obj.put("Lokal", locale);
        ss.add(ObjName + obj.toString());
        String ID = disp.getHttpResponse_POST(link, ss);
        return ID;
    } catch (Exception e) {
        return "";
    }
}

Above method is called from here:

public static String sendReq(Context ctx, String txt, String number) {
    JSONObject jsn = new JSONObject();
    try {
        jsn.put("TextPoruke", txt);
        jsn.put("BrTel", number);
        return JSONSend(ctx, jsn, "JSNSend=", "www.mysite.com");
    } catch (JSONException e1) {
        return "";
    }
}

Everything works fine on my Wamp server, but after moving my php code to webserver, nightmare started! Apparently, everything is sent the way it should be,but on serverside this php code is creating problems:

if(isset ($_POST['JSNSend']))
{
$argument = $_POST['JSNSend'];

$json = json_decode($argument,true);
$broj = $json['BrTel'];
$jsnLocale = $json['Lokal'];

it seems that result of "json_decode" is NULL, but $argument equals

{"\TextPoruke\": \"sometext\", \"BrTel\":\"111\"}

So passed JSON string seems ok, but for some reason it can't be decoded on webserver. Can anyone help me with this? Why it's not working?

  • 写回答

2条回答 默认 最新

  • dso0139 2011-06-11 16:41
    关注

    Seems like your JSON got escaped prematurely which triggers a bad syntax error.

    If $argument is in the format you state, then the following procedure would work:

    <?php
    
    $s = '{"\TextPoruke\": \"sometext\", \"BrTel\":\"111\"}';
    
    echo 'Without stripslashes:' . PHP_EOL;
    var_dump( json_decode( $s ) );
    
    echo 'With stripslashes:' . PHP_EOL;
    var_dump( json_decode( stripslashes($s) ) );
    
    ?>
    

    Result:

    Without stripslashes:
    NULL
    With stripslashes:
    object(stdClass)#1 (2) {
      ["TextPoruke"]=>
      string(8) "sometext"
      ["BrTel"]=>
      string(3) "111"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看