无法解析JavaScript中json对象内的值

时间:2018-10-15 05:59:05

标签: javascript json

我试图解析驻留在json对象中的值。以下是我的API调用的响应。

var responseData = {
  "statusCode": 200,
  "body": "{\"Errors\":\"\",\"Message\":null,\"OrgID\":0,\"Output\":\"{\\\"AllUserDetails\\\":{\\\"UserDetails \\\":[{\\\"UserID\\\":1012,\\\"UserName\\\":\\\"Arul, Velu\\\",\\\"EmpID\\\":null,\\\"Role\\\":\\\"SA\\\",\\\"RoleName\\\":\\\"Super Admin\\\",\\\"EmailID\\\":\\\"Arulvelu@gmail.com\\\",\\\"NT_UID\\\":\\\"ICS\\\\\\\\RNair\\\",\\\"Frm_UID\\\":\\\"Arulvelu@gmail.com\\\",\\\"Frm_PWD\\\":\\\"3ZD1KS+4gEL5RKsjlC5k0MIfaiV8k\\/ixZRS6NpNyu0g=\\\",\\\"Org_Id\\\":1,\\\"Instance_Model\\\":\\\"Dependent\\\",\\\"Org_Name\\\":\\\"India\\\",\\\"AccessCreate_Portfolio\\\":false,\\\"AccessEdit_Portfolio\\\":false,\\\"Customer\\\":2592,\\\"Customer_Name\\\":\\\"ICS\\\",\\\"Location\\\":null,\\\"Location_Name\\\":\\\"\\\",\\\"Userlevel\\\":2544,\\\"User_Level\\\":\\\"Analyst\\\",\\\"Country\\\":null,\\\"Designation\\\":\\\"Consultant\\\",\\\"ContactNo\\\":\\\"2133232123\\\",\\\"MobileNo\\\":null,\\\"JoinDate\\\":\\\"2016-01-06 00:00:00\\\",\\\"Access_ServerMon\\\":false,\\\"Access_IncidentMgmt\\\":true,\\\"Access_CMCMDB\\\":true,\\\"Access_CMDB\\\":true,\\\"Access_RM\\\":true,\\\"Access_KnowledgeMgmt\\\":true,\\\"Access_PortfolioMgmt\\\":true,\\\"Access_ServiceRequest\\\":true,\\\"Access_ServiceCatalog\\\":true,\\\"Access_NetworkMon\\\":false,\\\"Access_AssetMgmt\\\":true,\\\"Access_ProblemMgmt\\\":true,\\\"Access_ProjectMgmt\\\":true,\\\"Access_SkillPack\\\":true,\\\"Access_FinMgmt\\\":false,\\\"Access_SLA\\\":true,\\\"SLAMgr\\\":false,\\\"AccessType_ServerMon\\\":\\\"No Access\\\",\\\"AccessType_IncidentMgmt\\\":\\\"No Access\\\",\\\"AccessType_CMCMDB\\\":\\\"No Access\\\",\\\"AccessType_CMDB\\\":\\\"\\\",\\\"AccessType_RM\\\":\\\"\\\",\\\"AccessType_KnowledgeMgmt\\\":\\\"\\\",\\\"AccessType_PortfolioMgmt\\\":\\\"\\\",\\\"AccessType_ServiceRequest\\\":\\\"\\\",\\\"AccessType_ServiceCatalog\\\":\\\"\\\",\\\"AccessType_NetworkMon\\\":\\\"No Access\\\",\\\"AccessType_AssetMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProblemMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProjectMgmt\\\":\\\"No Access\\\",\\\"AccessType_SkillPack\\\":\\\"No Access\\\",\\\"AccessType_WorkOrder\\\":\\\"No Access\\\",\\\"AccessType_FinMgmt\\\":\\\"No Access\\\",\\\"AccessType_SLA\\\":\\\"\\\",\\\"Address\\\":\\\"2901 Butterfield Rd\\\\n2nd Flooor\\\",\\\"UserRemarks\\\":\\\"\\\",\\\"State\\\":\\\"\\\",\\\"City\\\":\\\"\\\",\\\"Pin\\\":\\\"\\\",\\\"Active\\\":true,\\\"UT_ID\\\":90,\\\"AccessType_SkillPack1\\\":\\\"No Access\\\",\\\"Access_EProc\\\":true,\\\"AccessType_Eproc\\\":\\\"General Access\\\",\\\"Entity_Access\\\":\\\"\\\",\\\"TimeZone\\\":\\\"(UTC - 06:00) CST - Central Standard Time\\\",\\\"GMT_TimeDiff\\\":-300,\\\"Access_EventMgmt\\\":false,\\\"AccessType_EventMgmt\\\":\\\"No Access\\\",\\\"Keyword_Access\\\":\\\"\\\",\\\"ManagerName\\\":\\\"Arulvelu\\\",\\\"ProjMgmt_InstanceAccess\\\":\\\"\\\",\\\"Manager_UserID\\\":null,\\\"IM_ExtendedRole\\\":\\\"RPT\\\",\\\"AM_ExtendedRole\\\":\\\"RPT\\\",\\\"CustomerMasterID\\\":11,\\\"OrgLogoActive\\\":null,\\\"CustomerLogoActive\\\":null,\\\"AM_EditTemplateID\\\":0,\\\"DesignationID\\\":1381,\\\"PT_userAccessLevel\\\":\\\"Analyst\\\",\\\"PT_workgroupId\\\":0,\\\"PT_CostcentreID\\\":0,\\\"Alternative_EmailID\\\":\\\" , , ,\\\",\\\"ManagerEmailID\\\":null,\\\"ManagerEmployeeID\\\":null}]}}\",\"OutputObject\":null,\"TokenID\":null}",
  "headers": {
    "cache-control": "private",
    "content-type": "application/json; charset=utf-8",
    "server": "Microsoft-IIS/8.5",
    "set-cookie": [
      "ASP.NET_SessionId=2ncl0ezht5; path=/; HttpOnly"
    ],
    "x-aspnet-version": "4.0.30319",
    "x-powered-by": "ASP.NET",
    "date": "Sat, 13 Oct 2018 20:12:39 GMT",
    "connection": "close",
    "content-length": "3314"
  }
}

我可以使用responseData.body获得主体值。但是我需要获取body.Output.AllUserDetails.UserDetails [0].UserID内部的值,这使我抛出一条错误,指出AllUserDetailsis未定义。谁能帮助我。

4 个答案:

答案 0 :(得分:4)

尝试一下:

JSON.parse(JSON.parse(responseData.body).Output).AllUserDetails['UserDetails '][0].UserID

请注意,UserDetails的api响应中有多余的空格,因此您不能使用点符号来访问它。

答案 1 :(得分:1)

简单的答案:

(\d)\1{3,}

然后获取所需的任何属性。

长答案:

对象内部的json是字符串,而不是对象。

答案 2 :(得分:0)

您需要多次JSON.parse,然后才能访问用户详细信息

var responseData = {
  "statusCode": 200,
  "body": "{\"Errors\":\"\",\"Message\":null,\"OrgID\":0,\"Output\":\"{\\\"AllUserDetails\\\":{\\\"UserDetails \\\":[{\\\"UserID\\\":1012,\\\"UserName\\\":\\\"Arul, Velu\\\",\\\"EmpID\\\":null,\\\"Role\\\":\\\"SA\\\",\\\"RoleName\\\":\\\"Super Admin\\\",\\\"EmailID\\\":\\\"Arulvelu@gmail.com\\\",\\\"NT_UID\\\":\\\"ICS\\\\\\\\RNair\\\",\\\"Frm_UID\\\":\\\"Arulvelu@gmail.com\\\",\\\"Frm_PWD\\\":\\\"3ZD1KS+4gEL5RKsjlC5k0MIfaiV8k\\/ixZRS6NpNyu0g=\\\",\\\"Org_Id\\\":1,\\\"Instance_Model\\\":\\\"Dependent\\\",\\\"Org_Name\\\":\\\"India\\\",\\\"AccessCreate_Portfolio\\\":false,\\\"AccessEdit_Portfolio\\\":false,\\\"Customer\\\":2592,\\\"Customer_Name\\\":\\\"ICS\\\",\\\"Location\\\":null,\\\"Location_Name\\\":\\\"\\\",\\\"Userlevel\\\":2544,\\\"User_Level\\\":\\\"Analyst\\\",\\\"Country\\\":null,\\\"Designation\\\":\\\"Consultant\\\",\\\"ContactNo\\\":\\\"2133232123\\\",\\\"MobileNo\\\":null,\\\"JoinDate\\\":\\\"2016-01-06 00:00:00\\\",\\\"Access_ServerMon\\\":false,\\\"Access_IncidentMgmt\\\":true,\\\"Access_CMCMDB\\\":true,\\\"Access_CMDB\\\":true,\\\"Access_RM\\\":true,\\\"Access_KnowledgeMgmt\\\":true,\\\"Access_PortfolioMgmt\\\":true,\\\"Access_ServiceRequest\\\":true,\\\"Access_ServiceCatalog\\\":true,\\\"Access_NetworkMon\\\":false,\\\"Access_AssetMgmt\\\":true,\\\"Access_ProblemMgmt\\\":true,\\\"Access_ProjectMgmt\\\":true,\\\"Access_SkillPack\\\":true,\\\"Access_FinMgmt\\\":false,\\\"Access_SLA\\\":true,\\\"SLAMgr\\\":false,\\\"AccessType_ServerMon\\\":\\\"No Access\\\",\\\"AccessType_IncidentMgmt\\\":\\\"No Access\\\",\\\"AccessType_CMCMDB\\\":\\\"No Access\\\",\\\"AccessType_CMDB\\\":\\\"\\\",\\\"AccessType_RM\\\":\\\"\\\",\\\"AccessType_KnowledgeMgmt\\\":\\\"\\\",\\\"AccessType_PortfolioMgmt\\\":\\\"\\\",\\\"AccessType_ServiceRequest\\\":\\\"\\\",\\\"AccessType_ServiceCatalog\\\":\\\"\\\",\\\"AccessType_NetworkMon\\\":\\\"No Access\\\",\\\"AccessType_AssetMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProblemMgmt\\\":\\\"No Access\\\",\\\"AccessType_ProjectMgmt\\\":\\\"No Access\\\",\\\"AccessType_SkillPack\\\":\\\"No Access\\\",\\\"AccessType_WorkOrder\\\":\\\"No Access\\\",\\\"AccessType_FinMgmt\\\":\\\"No Access\\\",\\\"AccessType_SLA\\\":\\\"\\\",\\\"Address\\\":\\\"2901 Butterfield Rd\\\\n2nd Flooor\\\",\\\"UserRemarks\\\":\\\"\\\",\\\"State\\\":\\\"\\\",\\\"City\\\":\\\"\\\",\\\"Pin\\\":\\\"\\\",\\\"Active\\\":true,\\\"UT_ID\\\":90,\\\"AccessType_SkillPack1\\\":\\\"No Access\\\",\\\"Access_EProc\\\":true,\\\"AccessType_Eproc\\\":\\\"General Access\\\",\\\"Entity_Access\\\":\\\"\\\",\\\"TimeZone\\\":\\\"(UTC - 06:00) CST - Central Standard Time\\\",\\\"GMT_TimeDiff\\\":-300,\\\"Access_EventMgmt\\\":false,\\\"AccessType_EventMgmt\\\":\\\"No Access\\\",\\\"Keyword_Access\\\":\\\"\\\",\\\"ManagerName\\\":\\\"Arulvelu\\\",\\\"ProjMgmt_InstanceAccess\\\":\\\"\\\",\\\"Manager_UserID\\\":null,\\\"IM_ExtendedRole\\\":\\\"RPT\\\",\\\"AM_ExtendedRole\\\":\\\"RPT\\\",\\\"CustomerMasterID\\\":11,\\\"OrgLogoActive\\\":null,\\\"CustomerLogoActive\\\":null,\\\"AM_EditTemplateID\\\":0,\\\"DesignationID\\\":1381,\\\"PT_userAccessLevel\\\":\\\"Analyst\\\",\\\"PT_workgroupId\\\":0,\\\"PT_CostcentreID\\\":0,\\\"Alternative_EmailID\\\":\\\" , , ,\\\",\\\"ManagerEmailID\\\":null,\\\"ManagerEmployeeID\\\":null}]}}\",\"OutputObject\":null,\"TokenID\":null}",
  "headers": {
    "cache-control": "private",
    "content-type": "application/json; charset=utf-8",
    "server": "Microsoft-IIS/8.5",
    "set-cookie": [
      "ASP.NET_SessionId=2ncl0ezht5; path=/; HttpOnly"
    ],
    "x-aspnet-version": "4.0.30319",
    "x-powered-by": "ASP.NET",
    "date": "Sat, 13 Oct 2018 20:12:39 GMT",
    "connection": "close",
    "content-length": "3314"
  }
}

var responseJSON = JSON.parse(JSON.stringify(responseData));
var responseJSONBody = JSON.parse(responseJSON.body)
var responseJSONBodyOutput = JSON.parse(responseJSONBody.Output)

console.log(responseJSONBodyOutput);

答案 3 :(得分:0)

尝试-

body.Output.AllUserDetails['UserDetails '][0].UserID