简体   繁体   English

如何将json转换为纯文本?

[英]How to convert json to plain text?

How to convert JSON to plain text?如何将 JSON 转换为纯文本?

{"Days":["is not a number"]} to Days is not a number. {"Days":["is not a number"]} to Days 不是数字。

Here is the code:这是代码:

$('.best_in_place').bind("ajax:error", function(jqXHR,error, errorThrown) {
  alert(error.responseText);
});

As you're using jQuery, this might help:当您使用 jQuery 时,这可能会有所帮助:

var result = '';
$.each(error.responseText, function(key, value) {
    result += key + ' ' + value;
});

This will also work and can easily be adjusted if the response holds multiple key-value-pairs.如果响应包含多个键值对,这也将起作用并且可以轻松调整。

Demo演示

Try before buy先试后买

Convert the response into JSON object and parse its key-value pair将响应转换为 JSON 对象并解析其键值对

var error = JSON.parse( error.responseText );
for( var name in error ) {
    console.log( name + " " + error[ name ] ); // Days is not a number
}

Convert any json to plain text.将任何 json 转换为纯文本。

Installation安装

npm install json-to-plain-text

Usage用法

var convert = require('json-to-plain-text');

var jsonData = {
    "place_id": "173937105",
    "osm_type": "way",
    "osm_id": "319992693",
    "lat": "17.861533866867224",
    "lon": "78.8081441896764",
    "display_name": "Satadar Nagar, Ward 116 Allapur, Hyderabad, Kukatpally mandal, Telangana, 500018, India",
    "address": {
        "neighbourhood": "Satadar Nagar",
        "suburb": "Ward 116 Allapur",
        "city": "Hyderabad",
        "county": "Kukatpally mandal",
        "state": "Telangana",
        "postcode": "500018",
        "country": "India",
        "country_code": "in"
    },
    "extratags": {},
    "namedetails": {},
    "boundingbox": [
        "17.8598497",
        "17.8623087",
        "78.8079136",
        "78.8082658"
    ],
    "distance": 2
}

var response = convert.toPlainText(jsonData);
console.log(response);

Output输出

place_id            : 173937105
osm_type            : way
osm_id              : 319992693
lat                 : 17.861533866867224
lon                 : 78.8081441896764
display_name        : Satadar Nagar, Ward 116 Allapur, Hyderabad, Kukatpally mandal, Telangana, 500018, India
address             :
neighbourhood       : Satadar Nagar
suburb              : Ward 116 Allapur
city                : Hyderabad
county              : Kukatpally mandal
state               : Telangana
postcode            : 500018
country             : India
country_code        : in
extratags           : {}
namedetails         : {}
boundingbox         : 17.8598497, 17.8623087, 78.8079136, 78.8082658
distance            : 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM