【问题标题】:How can i decode data from JSON.stringify我如何解码来自 JSON.stringify 的数据
【发布时间】:2016-03-28 08:00:56
【问题描述】:

我无法解码 json 数据,请帮帮我。 我正在.net 中从以下函数进行 json 编码:-

function(response){
    var jsonString = JSON.stringify(response.newhtml); //stringify the JSON created

    //Send the json data to server
    //---------------------------
    $.ajax({
        type: "POST",
        url: 'url',
        //dataType:"json",
        data: {
            LiRecData: jsonString
        },
    }).done(function(msg){
        $('#load_indicator_store').hide(); //Hide loading icon

        if(msg == 'Success'){
            $('#status_save').html('<span class="status_ok">Save.</span>');
        }
        else {
            $('#status_save').html('<span class="status_error">An error occurred. Please try again after sometime.</span>');
        }
    });
}

下面是解码函数

这里我无法解码这个$_POST['LiRecData']

$_POST['LiRecData'] =
"{"LinkedIn_URL": "https://in.linkedin.com/in/abcsingh","Full_Name": "Abc Singh (Abc - 7K Connections)","Headline": "A Senior Recruitment Professional - Connecting Employers with their prospective assets (Employee)","Company": "Charter Global","Location": "New Delhi Area, India","Industry": "Information Technology and Services","Email": "venky.s@interproeurope.com","Phone_Number": "+91-999034656 (Mobile)","Connection_Count": "500+6,336","Photo_URL": "https://media.licdn.com/mpr/mpr/sh1rinknp_400_400/p/3/0105/0b5/352/1ad670e.jpg","Summary": "A Senior Recruitment Professional with over 12+ years of overall experience, majorly in Talent/Client Acquisition, Recruitment, IT Staffing, Business Development, Account Management mainly in Staffing space.<br>
<br>
"Connecting Employers with their prospective assets (Employee)"<br>
<br>
Helped a Recruitment Startup to achieve his goal and vision.<br>
<br>
Connecting Dots and bridging between Talents and Dream Companies...<br>
<br>
My personal specialties include:<br>
<br>
Permanent/Contract IT Recruitment, Developing new strategies, Research, Client Acquisition, Business Development, Account Management, Client/Industry Mapping, Marketing, Business terms negotiation, Lead development, professional networking and Presentations.<br>
<br>
To find out how we can assist you or your business please contact me: <br>
<br>
<br>
Official No: +1-999-999-9933 x 513<br>
Official Email: abcgh@abclobal.com<br>
Personal E-mail: abc@gmail.com<br>
Cell: +91-999999-6656<br>
<br>
<br>
I am a [LION] - feel free to network with me<br>
<br>
Specialties: Winning New Business - Candidate Selection - Candidate Handling - Account Management - Recruitment Process - 360 Recruitment Cycle<br>
<br>
About Me: <br>
Abc Singh (Abc Singh), A Senior Recruitment Professional as an Expert in Recruitment, Technical Hiring, Business Development, Account Management, Client Acquisition, Client Delivery, with experience of 12+ years, adept at creative and cost-effective sourcing strategies with a goal of hiring outstanding candidates.","Work_Experience": [{"Title": "Business Development Manager - Staffing/Client Acquisition/Consulting","Company": "Abc","Date_From": "February 2016","Date_To": "Present","Locality": "New Delhi Area, India // Atlanta, GA","More": "Moving further, involved into the Business Development, Client Acquisition, Account Management, majorly in Staffing, Talent Acquisition, Consulting space for the client across Global market. <br>
"},{"Title": "HeadHunter / Candidate Manager / Researcher / Recruitment Specialist - DACH/UK/Europe.","Company": "Interpro People","Date_From": "August 2013","Date_To": "November 2015","Locality": "London/UK, Noida/India","More": "Recruiting for SAP positions viz; SAP SD, MM, WM/EWM, FI/CO, PP-PI-PM, QM, APO, ABAP/Basis, Business Intelligence experts (SAP BI, SAP BW), SAP BI-IP / Integrated Planning, SAP BusinessObjects and SAP BPC expert candidates and run the EU Enterprise Applications division.<br>
Involved into the 360 Recruitment, Candidate Management, Researching, Industry/Client Mapping. <br>
Using Social and professional Networking site to work ahead of job portals.<br>
Use/Search Innovating method of recruitment and implement within the organization."},{"Title": "Resource Manager","Company": "BeyondTek IT","Date_From": "October 2012","Date_To": "August 2013","Locality": "Placentia, CA 92870","More": "Recruitment, Resourcing, Talent Acquisition, Hiring, Technical Recruitment for IT, Software, Business Process, Product Development, Fortune 500 organizations. <br>
Involved into overall Recruitment."},{"Title": "Asst. Manager - Recruitment","Company": "Knowledge Momentum","Date_From": "August 2010","Date_To": "August 2012","Locality": "Noida &amp; North Atlanta, GA","More": ""},{"Title": "Asst. Manager - Recruitment","Company": "Izar Associates Inc","Date_From": "June 2008","Date_To": "July 2010","Locality": "","More": "Play a key role in this organization for the recruitment, resourcing and Full Recruitment Life Cycle. Known for its services and timely delivery with optimum quality. Handled the recruitment for Fortune 500 Organizations including some are in the list of Top 50 Fortune Clients."},{"Title": "Team Lead - IT Recruitment","Company": "Ling Technologies","Date_From": "March 2007","Date_To": "April 2008","Locality": "","More": ""},{"Title": "SENIOR IT RECRUITER","Company": "DatamanUSA, LLC","Date_From": "February 2006","Date_To": "February 2007","Locality": "","More": ""},{"Title": "Sr. Officer - Consulting","Company": "Tusthi Executive Access And Consultants","Date_From": "2005","Date_To": "2006","Locality": "","More": ""},{"Title": "SR. Customer Response Executive","Company": "Reliance Infostream Pvt Ltd","Date_From": "November 2003","Date_To": "May 2005","Locality": "","More": ""}],"Education": [{"Institution_Name": "Sikkim Manipal University","Degree": "MBA, HRM","Grade": "","Date_From": "2011","Date_To": "2013"},{"Institution_Name": "Chhatrapati Shahu Ji Maharaj University","Degree": "Bachelor of Arts (B.A.), Economics & English Literature","Grade": "","Date_From": "2000","Date_To": "2003"},{"Institution_Name": "Kendriya Vidyalaya","Degree": "10 & 12th, Economics, Geography, English","Grade": "","Date_From": "1985","Date_To": "1997"}]},"Top_Skills": [{"Endorse_Count": "7","Endorse_Name": "Entrepreneurship"},{"Endorse_Count": "44","Endorse_Name": "Business Development"},{"Endorse_Count": "16","Endorse_Name": "Account Management"},{"Endorse_Count": "8","Endorse_Name": "Client Acquisition"},{"Endorse_Count": "90","Endorse_Name": "Screening"},{"Endorse_Count": "85","Endorse_Name": "IT Recruitment"},{"Endorse_Count": "34","Endorse_Name": "Leadership"},{"Endorse_Count": "99+","Endorse_Name": "Talent Acquisition"},{"Endorse_Count": "61","Endorse_Name": "Management"},{"Endorse_Count": "99+","Endorse_Name": "Technical Recruiting"}]}"

function decodedata(){
    $data = utf8_encode($_POST['LiRecData']);
    $data = str_replace("'","",$data);

    $json = json_decode($data, true);
    print_r($json);
}

【问题讨论】:

  • 您将请求发送至url: 'url'
  • 正确格式化问题
  • 您能否将您的问题文本缩减为实际需要的内容?您无需在此处发布整个 JSON 数据!
  • 我只想解码 json 数据
  • 这似乎不是一个有效的 JSON。在这里查看:jsonformatter.curiousconcept.com

标签: php json


【解决方案1】:

您的问题是无效的 JSON 文本。

我已尝试修复 json。请注意,这很丑陋。很丑!

因此,将其作为输入:

$a = '{"LinkedIn /*trimmed data to fit this format*/ Recruiting"}]}';
// you use $a = $_POST['LiRecData'];

我所做的是尝试根据一些(少数)现有/正确元素重构 JSON:

$a = str_replace(["\n","\t",'<br>','</br>'],'',$a);       // remove new lines, tabs and br tags
$a = preg_replace ( '/([^"])\,([^"])/' , "$1___$2" , $a); // replace commas inside qoutes with SOMETHING
$a = preg_replace ( '/([^"])\:([^"])/' , "$1__|$2" , $a); // replace semicolon inside qoutes with SOMETHING ELSE
$a = str_replace('"','',$a);                              // remove all qoutes
$a = str_replace(':','":"',$a);                           // quote all semicolons 
$a = str_replace(',','","',$a);                           // quote all commas
$a = preg_replace ( '/([\{])([a-zA-Z0-9])/' , "$1\"$2" , $a); // {A => {"A
$a = preg_replace ( '/([\:])\"\s([\[])/' , "$1$2" , $a);      // : " [ => : [
$a = preg_replace ( '/([a-zA-Z0-9])([\}])/' , "$1\"$2" , $a); // adas} => adas"}
$a = preg_replace ( '/([\.])(\s{0,})([\}])/' , "$1\"$3" , $a);
$a = preg_replace ( '/([\"])(\s{1,})([a-zA-Z0-9])/' , "$1$3" , $a); // "   Key" => "Key" trim keys
$a = str_replace(':" }',':"" }',$a);
$a = str_replace(']","','],"', $a);
$a = str_replace("___",",",$a);             // revert comment 2
$a = str_replace("__|",":",$a);             // revert comment 3
$a = str_replace(']}","Top', '],"Top',$a);  //manual fix for TopSkils

print_r(json_decode($a,true));

你可以在这里看到它:

https://3v4l.org/QdaiV

【讨论】:

  • 干得好亚历克斯,但不禁认为这个问题应该在得到答案之前重新处理,并且继承了这种混乱的可怜的开发人员
  • @simpsojo,确实如此。但在我最近看到的所有低质量问题中,至少这个问题付出了一些努力。是的,这仍然是一个“糟糕”的问题(也许过于本地化)。但是:OP 确实问得很好,确定了问题(json)并发布了相关代码/信息。是的,问题要深得多:不应该像/那样使用 jsons(传输 html)。
  • 我确实花了很多钱。也许出于个人背景。我曾经遇到过类似的问题(从外部 WSDL 返回的 XML 字段中的 json),它太糟糕了,我不得不解析它的两倍……而且我记得所有的挫败感……所以我决定帮忙;)
  • @AlexTartan 足够公平,公平竞争
猜你喜欢
  • 2023-02-06
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2012-02-08
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多