最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert json to plain text? - Stack Overflow

programmeradmin2浏览0评论

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);
});

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);
});
Share Improve this question edited Sep 9, 2013 at 16:23 insertusernamehere 23.6k10 gold badges91 silver badges128 bronze badges asked Sep 9, 2013 at 16:16 BrunoBruno 6,47917 gold badges71 silver badges105 bronze badges 1
  • is it always a object with a key-value pair, and the value is a array with a length of one? – Math chiller Commented Sep 9, 2013 at 16:20
Add a ment  | 

3 Answers 3

Reset to default 2

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
}

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

Use json-to-plain-text npm module

发布评论

评论列表(0)

  1. 暂无评论