search

Home  >  Q&A  >  body text

javascript - Problem with native js ajax getting data and converting it into json object

Use ajax written natively in js to obtain a data list, and the order is normal

xmlHttp.responseText The order here is normal

But when I convert the json string into a json object, the order is not the original order

var json = eval('(' + xmlHttp.responseText + ')');

Is there any way to maintain the original output order after conversion?

高洛峰高洛峰2780 days ago472

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-19 10:40:41

    I don’t quite understand, what do you mean? Has the data order changed? ?
    Actually, there are several ways toJson, you can try them all.

    // 1
    function toJson(json){
        return json ? (new Function("return "+ json))() : {}
    }
    toJson(xmlHttp.responseText)
    
    // 2
    JSON.parse(xmlHttp.responseText)
    
    // 3 就是你那种

    reply
    0
  • Cancelreply