开发者

javascript library interprets strings as JSON objects. Any ideas?

开发者 https://www.devze.com 2023-03-29 04:43 出处:网络
Here\'s the query result: [\"Language1\",\"anotherLanguage\",\"yetanotherlangugae\"] Javascriptmvc\'s model classes findAll method returns (via success callback) an array of javascript objects. The f

Here's the query result: ["Language1","anotherLanguage","yetanotherlangugae"]

Javascriptmvc's model classes findAll method returns (via success callback) an array of javascript objects. The framework takes care of converting the text returned by the server to an array of javascript objects. If the query result is as above, each string in the array is treated as a an associative array with the key the index of the character and the value the character. The result is shown below.

The results of a call to JSON.stringify are shown below.

[{"0":"L","1":"a","2":"n","3":"g","4":"u","5":"a","6":"g","7":"e","8":"1"},{"0":"a","1":"n","2":"o","3":"t","4":"h","5":"e","6":"r","7":"L","8":"a","9":"n","10":"g","11":"u","12":"a","13":"g","14":"e"},{"0":"y","1":"e","2":"t","3":"开发者_高级运维a","4":"n","5":"o","6":"t","7":"h","8":"e","9":"r","10":"l","11":"a","12":"n","13":"g","14":"u","15":"g","16":"a","17":"e"}]

The query is done by a javascriptmvc model and the result returned shown above passed into the success callback. The query result is transformed into an array of three JSON objects. What's going on here?

This is not an issue of calling JSON.parse. The response has already been parsed into a json object by the time it has been handed to me via the success callback.

The result should be an array of JSON objects that, if serialized into a string in a natural way, looks just like the original response from the server. That's not the case here, and hence the problem.

Here's the snippet that does the actual findAll query:

  $.ajax({
        url: '/language',
        type: 'get',
        dataType: 'json',
        data: params,
        success: this.callback(['wrapMany',success]),
        error: error
   });


Found the answer.

When you use javascriptmvc to generate a model, it generates something like the following for the findAll method:

    $.ajax({
        url: '/language',
        type: 'get',
        dataType: 'json',
        data: params,
        //success: this.callback(['wrapMany',success]),
        success: success,
        error: error
   });

Notice the commented out line. It calls wrapMany on the response. The expected response is an array of JSON objects, not strings. If the response is an array of strings, each string is converted into a JSON object, and hence the result you see in the question.

So instead of returning ["lan1", "lan2"] the server should respond with something like [{"language" : "lan1"}, {"language" : "lan2"}]


I think you're looking for json.parse(json_string).
json.stringify() is doing what it's supposed to - converting what Javascript sees as an array of characters into a JSON-style array of characters.

object = json.parse(string);
string = json.stringify(object);


maybe if your query returned:

 "{ result : ["Language1","anotherLanguage","yetanotherlangugae"] }"

you would get what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消