English 中文(简体)
从 Django 视图中的json 返回的数据中获取元素
原标题:retrieving elements from data returned as json from django view

在我 django 视图中,我使用 sprojson 将一些搜索结果转换为 json

vals = [( supposed to be a toaster. , 8),( we can do more than one thing. ,14),("we could make a bicycle.",51)]

result={ results :vals}

serialized = simplejson.dumps(result)

序列化gt;

{"msg": "success!.",  "results": [["supposed to be a toaster.", 8], ["we can do more than one thing.", 14], [" we could make a bicycle.", 51]]}

我可以将这个序列数据发送到 Javascript 函数

return HttpResponse(serialized, mimetype="application/json")

在我的javascript 函数(使用jquery)中,我可以以下列方式检索数据:

var data = $.parseJSON(res.responseText);
var results = data[ results ]

我要以以下格式显示结果:

8  -- supposed to be a toaster. 
14 -- we can do more than one thing
51 -- we could make a bicycle

我如何用 javacrip 来做到这一点? javacrip 变量 结果 包含 s

supposed to be a toaster.,8,we can do more than one thing.,14,we could make a bicycle.,51,

Will I have to use regex to separate the items?or is there a better solution? What makes use of regex difficult is that,the strings may sometimes contain numbers .

< 强力 > 编辑 < /强 >

感谢Priyank和Alexey28的答复,

for(var item in results) {
    var time = results[item][1];
    console.log( time= +time);
    var resStr =results[item][0];
    console.log( resStr= +resStr);
    formatedResult += time+ " --- " + resStr+ <br> ;
}
$( #showresults ).html(formatedResult);
最佳回答

可变数据将包含数组, 因此您可以 :

var formatedResult = "";
for(var i = 0; i < data.length; i++) {
    var item = data[i];
    formatedResult += item[1] + " --- " + item[0];
}
// Set html for you <div id="resultOutput"></div>:
jQuery("div#resultOutput").html(formatedResult);
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签