简体   繁体   中英

How to convert json to plain text?

How to convert JSON to plain text?

{"Days":["is not a number"]} to Days is not a number.

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:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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