dounuo1881 2013-11-04 13:16
浏览 37
已采纳

使用html标签接收json在PHP中失败

I have a website that sends and receives string in JSON format from my Java REST server with jersey. Everything works fine until I'm trying to receive a json object with html tags.

A println on my java server tells me that this data has ben sent:

data sent: {"text": "Wij zijn Pixel Apps, ook wel bekend als Groep 6.<br />
Samen met onze 6 groepsleden verzorgen wij het reilen en zijlen op Ford Lommel Proving  Grounds.<br />
<br />
<b>Korte inleiding</b><br />
<p>Onze taak bestaat er uit een functionele applicatie te maken binnen Windows 8. De app bestaat er uit de chauffeurs te begeleiden op hun testritten.<br />De chauffeurs worden onder andere geholpen bij het bekijken van hun routineplan, het bijhouden van notities en het overzetten van de resultaten naar het hoofdgebouw.</p>
<b>Bijkomende hoort natuurlijk het onderhouden van deze website.</b>
<p>Zoals u kan zien vind u hierboven het navigatiemenu.<br />
Voor meer informatie over ons project kan u terecht bij <i>Over ons</i><br />
Wenst u contact op te nemen? U kan zich wenden naar het tabblad <i>Contact</i><br />
Indien u meer over de individuele groepsleden wil weten kan u terecht bij <i>Leden</i><br />
Als u meer informatie wenst over ons project, gelieve contact op te nemen met ons en wij verzorgen uw verzoek.</p>
<b>Happy browsing!</b>"}

It's basically a simple json with one variable "text" and as content some HTML formatted content. I've googled my issue and it seems that this should work fine.

Here's my java GET method that fails to send json with html tags in it's content:

@GET
@Path("gettext")
@Produces("application/json")
public String getJson(@QueryParam("id") String id, @QueryParam("taalcode") String taalcode) {
    Connectie c  = new Connectie();
    try
    {
        c.openConnectie();
        String content = c.getCms(id, taalcode);
        if (content == null || content.equals("")) {
            content = "{ \"text\" : \"Geen tekst gevonden.\" }";
        }
        System.out.println("data send: "+content);
        return content;
    }
    catch(Exception e)
    {
        System.out.println("data send: { \"text\" : \"Server error, sorry.\" }");
        return "{ \"text\" : \"Server error, sorry.\" }";
    }
}

My put method successfully receives a json with html tags in it's content. Here's how I receive my json objects in PHP (which again works if no html tags are present):

public function getCMS($id) {
    $taalcode = '';
    if($this->session->userdata('language') == 'nederlands') {
        $taalcode = 'NL';
    } else {
        $taalcode = 'EN';
    }
    $curl_instance = curl_init();
    curl_setopt($curl_instance, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_instance, CURLOPT_URL, 'http://192.168.0.251:8084/Groep1/webresources/cmspost/gettext?id='.$id.'&taalcode='.$taalcode);

    try {
        $data = json_decode(curl_exec($curl_instance), true);
        if ($data == null) {
            $data['text'] = "Altough I set a string in my java get method if it's null, this message is always printed";
        }
        return $data;
    } catch (HttpException $ex) {
        $data['text'] = $ex;
        return $data;
    }
}

In PHP I test if ($data == null) which is always true, even though I set a string manually in my GET method if appears to be null before sending the string.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doudou521125 2013-11-04 13:20
    关注

    The problem is not with HTML. The problem is that JSON does not allow multi-line strings. If you remove the line breaks, your JSON works fine.

    NB that you really should use a JSON library for building JSON, rather than doing it yourself, because it will deal with this kind of issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系