


Problems with json_decode garbled characters and NULL_javascript skills
具体内容请看下文吧。
写接口的同学应该会经常遇到数据格式的转换,这时候必不可少的两个函数就是json_encode()和json_decode()。
这两个函数使用的时候有很多的主要事项,在这里我来说一下json_decode()。
json_decode(): 对JSON 格式的字符串进行解码,接受一个JSON 格式的字符串并且把它转换为 PHP 变量。
(1)将数据转换成数组之后,打印会显示NUll:
原因之一json_decode只支持utf-8.
iconv('gbk','utf-8', $result_string);用iconv函数将写入数据的gbk编码格式转换为要输出的utf-8编码格式,若原本的数据是utf-8格式,则不用此步骤,否则还会出现乱码
原因之二:json字符串必须以双引号包含
str_replace("'", '"', $result_string);//将json数据中的单引替换成双引
原因之三:不能有多余的逗号 如:[1,2,]
用正则替换掉,preg_replace('/,\s*([\]}])/m', '$1', $result_string);
(2)将数据转换成数组或者在转换成json格式数据之后,会显示乱码:
这时候要用到urlencode()和urldecode()
以下是我的代码,经试验有效
if(file_exists($result['save_path'])){ $contents=file_get_contents($result['save_path']);//将一个文件的内容写入,文件是utf-8格式,里面是json格式的数据 //$getcontent = iconv("gbk", "utf-8//ignore",$contents);//若文件原本是utf-8格式,无需转换 $getcontent=str_replace("'", '"',$contents);//将单引替换成双引 preg_replace('/,\s*([\]}])/m', '$1', $getcontent);//去掉多余的逗号 $new_array=array(); $new_array=json_decode($getcontent,true); $res=array(); foreach ($new_array as $key=>$val){ foreach ($new_array[$key]['items'] as $k=>$v){ if($k<$row){ $res[$k]['position']=$v['position']; $res[$k]['distance']=$v['distance']; $res[$k]['title']=urlencode($v['title']); $res[$k['vicinity']=urlencode($v['vicinity']); } } } if($res){ $new_res['items']=$res; }else{ $new_res['items']=""; } echo urldecode(json_encode($new_res)); }
以上内容是小编给大家分享的有关json_decode乱码及NULL的问题,希望对大家有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any

Bring matrix movie effects to your page! This is a cool jQuery plugin based on the famous movie "The Matrix". The plugin simulates the classic green character effects in the movie, and just select a picture and the plugin will convert it into a matrix-style picture filled with numeric characters. Come and try it, it's very interesting! How it works The plugin loads the image onto the canvas and reads the pixel and color values: data = ctx.getImageData(x, y, settings.grainSize, settings.grainSize).data The plugin cleverly reads the rectangular area of the picture and uses jQuery to calculate the average color of each area. Then, use

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.
