2012-11-28 45 views
2

我有一個jQuery http請求,它以json字符串的形式返回對象。在這裏,我必須做的是將json轉換爲jquery對象,並將值賦給html元素......以下是要轉換的對象的結構。將json字符串轉換爲jquery中的對象

var Userrights = { 

      Id: // an id element 
      ScreenRights: // this is a list with following elements 
      { 
       ScreenName: 
       Create: 
       Read: 
       Update: 
       Delete: 
       Approve: 
       Access: 
       Print: 
       Email: 
      }; 
     }; 

現在,當我收到一個JSON字符串,如以下

{ 
    "Id": "Manager", 
    "ScreenRights": [{ 
     "ScreenName": "CustomerScreen", 
     "Create": true, 
     "Read": false, 
     "Update": true, 
     "Delete": false, 
     "Approve": true, 
     "Access": true, 
     "Print": true, 
     "Email": true 
    }, 
    { 
     "ScreenName": "TraderScreen", 
     "Create": true, 
     "Read": false, 
     "Update": true, 
     "Delete": false, 
     "Approve": true, 
     "Access": true, 
     "Print": true, 
     "Email": true 
    }, 
    { 
     "ScreenName": "DistributorScreen", 
     "Create": true, 
     "Read": false, 
     "Update": true, 
     "Delete": false, 
     "Approve": true, 
     "Access": true, 
     "Print": true, 
     "Email": true 
    }] 
}​ 

現在我將如何轉換這個字符串上述對象結構。

+0

什麼是你的問題? JSON有幾乎所需的形式,所以只需解析它。 – Sirko

回答

0

的Jquery:

var obj = $.parseJSON(string); 
console.log(obj); 

隨着中說,如果你正在使用jQuery的阿賈克斯,你可以只設置dataType:'json'

+0

謝謝。但你可以直接告訴我我該如何訪問該對象中的列表或該對象中的元素。我怎麼可以在這個列表中循環。 – user1811801

相關問題