我無法找到如何從data.items獲取第一個也是唯一的元素,我不想使用foreach函數,因爲我有那裏只有一個元素。如何從data.items(getJson funcion)獲取第一個(也是唯一一個)元素
0
A
回答
3
這樣,大概是:
var myOneItem = data.items[0];
如果
- 你的對象是已經被解析這將工作(這應該是使用的getJSON的名稱並不意味着這種情況)
- 有一個
items
財產 - 此屬性的值是一個數組
編輯:從您的評論似乎你需要:
for (var key in data) {
var val = data[key];
}
// you can use val here
1
試試這個:
... success: function (data) {
var oneItem = data[0].items;
}
編輯:
這裏是整個代碼
$.ajax({
url: "functionURL",
type: "GET/POST",
data: {functionArgument_Name:passingArgument_name},
success: function (data) {
var oneItem = data..;
}
});
所以,如果你有這樣的事情
public JsonResult getOneItem(string name)
{
Dictionary<String, String> items= new Dictionary<string, string>();
items.Add("items",name);
return Json(items, JsonRequestBehavior.AllowGet);
}
你的AJAX應該是這個樣子:
$.ajax({
url: "getOneItem",
type: "POST",
data: {name:"abcde"},
success: function (data) {
var oneItem = data.items;
}
});
+0
如果json只有一個元素會怎麼樣?該json鏈接是這樣的:http://students.info.uaic.ro/~calin.chifan/api/compartiment/sertar/obiect/add.json?nume=creioane&descriere=bunica&id_sertar=1000這沒有用[0] – raducup
相關問題
- 1. 在滾動div中獲取第一個也是最後一個可見元素
- 2. 從PDOStatement獲取第一個元素
- 3. List <>獲取下一個元素或獲取第一個
- 4. 獲取一個元組的第一個元素,在元組
- 5. 如何獲取第一個元素和最後一個元素使用django,Location.objects.all()
- 6. PHP獲取第一個數組元素
- 7. RxSwift throttle()獲取第一個元素
- 8. 獲取json的第一個元素
- 9. Simplehtmldom獲取第一個元素
- 10. 如何獲取第一個也是唯一一個具有特定屬性的對象?
- 11. jsoup:如何獲得第一個元素?
- 12. 如何檢索列表的第一個元素的唯一值?
- 13. 獲取IEnumerable項的第一個元素
- 14. 如何獲取從一個表和另一個唯一數據
- 15. 檢查數組的第一個元素需要是唯一的
- 16. 如何從Java中的枚舉中獲取第一個元素?
- 17. 如何從Jena SPAQL結果集中獲取第一個元素
- 18. 如何從.net的IEnumerable <T>獲取第一個元素?
- 19. 如何從排序列表中獲取第一個元素?
- 20. 我如何++獲得一套只是第一個元素在C
- 21. 從nhibernate獲取集合獲取第一個元素
- 22. ActionScript 3:從數組的第一個元素中獲取第一個字符
- 23. 獲取一個元素
- 24. 如何從此JSON數據中的第二個元素獲取第一個值?
- 25. 如何獲得第一個元素值在一個div在python
- 26. 向後切元組 - 如何處理獲取第一個元素?
- 27. 從列表中獲取第3個元素的第一個元素和第二個元素 - Haskell
- 28. 獲取基於一個子唯一列表元素
- 29. SSRS或SQL傳遞一組唯一的第一個元素
- 30. 獲取一個元素
不[]工作。我得到這個錯誤:無法讀取未定義的屬性'0' – raducup
這意味着你沒有items屬性。 –
如果json只有一個元素會怎麼樣? json鏈接是這樣的:students.info.uaic.ro/~calin.chifan/api/compartiment/sertar/...沒有用[0] – raducup