我有一个json字符串,我想要解析。
>>> test = json.dumps('{"content":{"search_highlight":{},"message_exchanged":{"messagesOnlyToViewee":true,"messagesOnlyToViewer":true},"Certifications":{"certsMpr":{},"empty":{}},"lix_treasury_callout":"B","network_overview":{"lix_deferLoad":"B","lix_showDetail":"control","distance":3,"lix_deferOnload":"B","allow_pivot_search":false,"i18n_S_NETWORK":"xyz\'s Network","facets":{"skill_explicit":{"data":[{"count":5,"name":"Equity Research","value":"2112"},{"count":5,"name":"Equities","value":"462"},{"count":5,"name":"Portfolio Management","value":"480"},{"count":4,"name":"Financial Markets","value":"1371"},{"count":4,"name":"Derivatives","value":"814"},{"count":3,"name":"Mutual Funds","value":"1043"},{"count":3,"name":"Financial Modeling","value":"1245"},{"count":3,"name":"MIS","value":"1303"},{"count":3,"name":"Negotiation","value":"256"},{"count":3,"name":"Trading","value":"283"}],"i18n_skill_explicit":"Skills"},"geo_region":{"data":[{"count":39,"name":"India","value":"in:0"},{"count":30,"name":"Mumbai Area, India","value":"in:7150"},{"count":1,"name":"Hyderabad Area, India","value":"in:6508"},{"count":1,"name":"Chandigarh Area, India","value":"in:6879"},{"count":1,"name":"Kolkata Area, India","value":"in:7003"},{"count":1,"name":"Moradabad Area, India","value":"in:7046"},{"count":1,"name":"Lucknow Area, India","value":"in:7093"},{"count":1,"name":"Bengaluru Area, India","value":"in:7127"},{"count":1,"name":"New Delhi Area, India","value":"in:7151"},{"count":1,"name":"Jaipur Area, India","value":"in:7287"}],"i18n_geo_region":"Location"},"current_company":{"data":[{"count":5,"name":"L&T Mutual Fund","value":"919608"},{"count":2,"name":"IDFC","value":"21175"},{"count":1,"name":"Indiabulls","value":"10359"},{"count":1,"name":"Innodata Inc.","value":"10827"},{"count":1,"name":"Tangoe","value":"11341"},{"count":1,"name":"Philippine National Bank","value":"17277"},{"count":1,"name":"bc","value":"164575"},{"count":1,"name":"Metropolis Health Services (I) Ltd","value":"236316"},{"count":1,"name":"India Infoline Ltd","value":"239538"},{"count":1,"name":"Wadhwa Group","value":"312891"}],"i18n_current_company":"Company"},"education":{"i18n_education":"School","data":[{"count":8,"name":"University of Mumbai","value":"13663"},{"count":1,"name":"Anna University","value":"13412"},{"count":1,"name":"Jawaharlal Nehru Technological University","value":"13520"},{"count":1,"name":"Karnataka State Open University","value":"13530"},{"count":1,"name":"University of Allahabad","value":"13652"},{"count":1,"name":"University of Lucknow","value":"13661"},{"count":1,"name":"West Bengal University of Technology","value":"13686"},{"count":1,"name":"University of Calcutta","value":"20302"},{"count":1,"name":"Welingkar Institute of Management","value":"21296"},{"count":1,"name":"The Institute of Chartered Accountants of India","value":"22460"}]},"industry":{"data":[{"count":22,"name":"Financial Services","value":"43"},{"count":4,"name":"Human Resources","value":"137"},{"count":2,"name":"Real Estate","value":"44"},{"count":2,"name":"Information Technology and Services","value":"96"},{"count":2,"name":"Capital Markets","value":"129"},{"count":1,"name":"Telecommunications","value":"8"},{"count":1,"name":"Hospital & Health Care","value":"14"},{"count":1,"name":"Banking","value":"41"},{"count":1,"name":"Investment Management","value":"46"},{"count":1,"name":"Education Management","value":"69"}],"i18n_industry":"Industry"}},"i18n_not_allow_conn_browse":"xyz does not allow connection browsing.","link__overview_detail":"/profile/profile\\u002dv2\\u002dnetwork\\u002doverview\\u002ddetail?id=131506997&srchFacet=search_facet_param&srchFacetValue=search_facet_value_param&facetResultCount=search_facet_count_param","i18n_count_string":"500+"}}}')
>>> test1 = json.dumps(test)
>>> test2 = json.loads(test1)
>>> test2['content']
Traceback (most recent call last):
File "<pyshell#129>", line 1, in <module>
test2['content']
TypeError: string indices must be integers
我不明白为什么会发生这个错误。如果我错过了什么,请告诉我。
答案 0 :(得分:3)
您对JSON进行了三重编码,但只进行了一次解码。
在拥有可在json.loads()
上编制索引的Python对象之前,您需要另外两个 'content'
调用:
>>> test = json.dumps('{"content":{"search_highlight":{},"message_exchanged":{"messagesOnlyToViewee":true,"messagesOnlyToViewer":true},"Certifications":{"certsMpr":{},"empty":{}},"lix_treasury_callout":"B","network_overview":{"lix_deferLoad":"B","lix_showDetail":"control","distance":3,"lix_deferOnload":"B","allow_pivot_search":false,"i18n_S_NETWORK":"xyz\'s Network","facets":{"skill_explicit":{"data":[{"count":5,"name":"Equity Research","value":"2112"},{"count":5,"name":"Equities","value":"462"},{"count":5,"name":"Portfolio Management","value":"480"},{"count":4,"name":"Financial Markets","value":"1371"},{"count":4,"name":"Derivatives","value":"814"},{"count":3,"name":"Mutual Funds","value":"1043"},{"count":3,"name":"Financial Modeling","value":"1245"},{"count":3,"name":"MIS","value":"1303"},{"count":3,"name":"Negotiation","value":"256"},{"count":3,"name":"Trading","value":"283"}],"i18n_skill_explicit":"Skills"},"geo_region":{"data":[{"count":39,"name":"India","value":"in:0"},{"count":30,"name":"Mumbai Area, India","value":"in:7150"},{"count":1,"name":"Hyderabad Area, India","value":"in:6508"},{"count":1,"name":"Chandigarh Area, India","value":"in:6879"},{"count":1,"name":"Kolkata Area, India","value":"in:7003"},{"count":1,"name":"Moradabad Area, India","value":"in:7046"},{"count":1,"name":"Lucknow Area, India","value":"in:7093"},{"count":1,"name":"Bengaluru Area, India","value":"in:7127"},{"count":1,"name":"New Delhi Area, India","value":"in:7151"},{"count":1,"name":"Jaipur Area, India","value":"in:7287"}],"i18n_geo_region":"Location"},"current_company":{"data":[{"count":5,"name":"L&T Mutual Fund","value":"919608"},{"count":2,"name":"IDFC","value":"21175"},{"count":1,"name":"Indiabulls","value":"10359"},{"count":1,"name":"Innodata Inc.","value":"10827"},{"count":1,"name":"Tangoe","value":"11341"},{"count":1,"name":"Philippine National Bank","value":"17277"},{"count":1,"name":"bc","value":"164575"},{"count":1,"name":"Metropolis Health Services (I) Ltd","value":"236316"},{"count":1,"name":"India Infoline Ltd","value":"239538"},{"count":1,"name":"Wadhwa Group","value":"312891"}],"i18n_current_company":"Company"},"education":{"i18n_education":"School","data":[{"count":8,"name":"University of Mumbai","value":"13663"},{"count":1,"name":"Anna University","value":"13412"},{"count":1,"name":"Jawaharlal Nehru Technological University","value":"13520"},{"count":1,"name":"Karnataka State Open University","value":"13530"},{"count":1,"name":"University of Allahabad","value":"13652"},{"count":1,"name":"University of Lucknow","value":"13661"},{"count":1,"name":"West Bengal University of Technology","value":"13686"},{"count":1,"name":"University of Calcutta","value":"20302"},{"count":1,"name":"Welingkar Institute of Management","value":"21296"},{"count":1,"name":"The Institute of Chartered Accountants of India","value":"22460"}]},"industry":{"data":[{"count":22,"name":"Financial Services","value":"43"},{"count":4,"name":"Human Resources","value":"137"},{"count":2,"name":"Real Estate","value":"44"},{"count":2,"name":"Information Technology and Services","value":"96"},{"count":2,"name":"Capital Markets","value":"129"},{"count":1,"name":"Telecommunications","value":"8"},{"count":1,"name":"Hospital & Health Care","value":"14"},{"count":1,"name":"Banking","value":"41"},{"count":1,"name":"Investment Management","value":"46"},{"count":1,"name":"Education Management","value":"69"}],"i18n_industry":"Industry"}},"i18n_not_allow_conn_browse":"xyz does not allow connection browsing.","link__overview_detail":"/profile/profile\\u002dv2\\u002dnetwork\\u002doverview\\u002ddetail?id=131506997&srchFacet=search_facet_param&srchFacetValue=search_facet_value_param&facetResultCount=search_facet_count_param","i18n_count_string":"500+"}}}')
>>> test1 = json.dumps(test)
>>> test2 = json.loads(test1)
>>> test3 = json.loads(test2)
>>> test4 = json.loads(test3)
>>> test4['content']
{u'message_exchanged': {u'messagesOnlyToViewee': True, u'messagesOnlyToViewer': True}, u'search_highlight': {}, u'lix_treasury_callout': u'B', u'Certifications': {u'certsMpr': {}, u'empty': {}}, u'network_overview': {u'lix_showDetail': u'control', u'distance': 3, u'link__overview_detail': u'/profile/profile-v2-network-overview-detail?id=131506997&srchFacet=search_facet_param&srchFacetValue=search_facet_value_param&facetResultCount=search_facet_count_param', u'allow_pivot_search': False, u'facets': {u'current_company': {u'i18n_current_company': u'Company', u'data': [{u'count': 5, u'name': u'L&T Mutual Fund', u'value': u'919608'}, {u'count': 2, u'name': u'IDFC', u'value': u'21175'}, {u'count': 1, u'name': u'Indiabulls', u'value': u'10359'}, {u'count': 1, u'name': u'Innodata Inc.', u'value': u'10827'}, {u'count': 1, u'name': u'Tangoe', u'value': u'11341'}, {u'count': 1, u'name': u'Philippine National Bank', u'value': u'17277'}, {u'count': 1, u'name': u'bc', u'value': u'164575'}, {u'count': 1, u'name': u'Metropolis Health Services (I) Ltd', u'value': u'236316'}, {u'count': 1, u'name': u'India Infoline Ltd', u'value': u'239538'}, {u'count': 1, u'name': u'Wadhwa Group', u'value': u'312891'}]}, u'geo_region': {u'data': [{u'count': 39, u'name': u'India', u'value': u'in:0'}, {u'count': 30, u'name': u'Mumbai Area, India', u'value': u'in:7150'}, {u'count': 1, u'name': u'Hyderabad Area, India', u'value': u'in:6508'}, {u'count': 1, u'name': u'Chandigarh Area, India', u'value': u'in:6879'}, {u'count': 1, u'name': u'Kolkata Area, India', u'value': u'in:7003'}, {u'count': 1, u'name': u'Moradabad Area, India', u'value': u'in:7046'}, {u'count': 1, u'name': u'Lucknow Area, India', u'value': u'in:7093'}, {u'count': 1, u'name': u'Bengaluru Area, India', u'value': u'in:7127'}, {u'count': 1, u'name': u'New Delhi Area, India', u'value': u'in:7151'}, {u'count': 1, u'name': u'Jaipur Area, India', u'value': u'in:7287'}], u'i18n_geo_region': u'Location'}, u'industry': {u'i18n_industry': u'Industry', u'data': [{u'count': 22, u'name': u'Financial Services', u'value': u'43'}, {u'count': 4, u'name': u'Human Resources', u'value': u'137'}, {u'count': 2, u'name': u'Real Estate', u'value': u'44'}, {u'count': 2, u'name': u'Information Technology and Services', u'value': u'96'}, {u'count': 2, u'name': u'Capital Markets', u'value': u'129'}, {u'count': 1, u'name': u'Telecommunications', u'value': u'8'}, {u'count': 1, u'name': u'Hospital & Health Care', u'value': u'14'}, {u'count': 1, u'name': u'Banking', u'value': u'41'}, {u'count': 1, u'name': u'Investment Management', u'value': u'46'}, {u'count': 1, u'name': u'Education Management', u'value': u'69'}]}, u'education': {u'data': [{u'count': 8, u'name': u'University of Mumbai', u'value': u'13663'}, {u'count': 1, u'name': u'Anna University', u'value': u'13412'}, {u'count': 1, u'name': u'Jawaharlal Nehru Technological University', u'value': u'13520'}, {u'count': 1, u'name': u'Karnataka State Open University', u'value': u'13530'}, {u'count': 1, u'name': u'University of Allahabad', u'value': u'13652'}, {u'count': 1, u'name': u'University of Lucknow', u'value': u'13661'}, {u'count': 1, u'name': u'West Bengal University of Technology', u'value': u'13686'}, {u'count': 1, u'name': u'University of Calcutta', u'value': u'20302'}, {u'count': 1, u'name': u'Welingkar Institute of Management', u'value': u'21296'}, {u'count': 1, u'name': u'The Institute of Chartered Accountants of India', u'value': u'22460'}], u'i18n_education': u'School'}, u'skill_explicit': {u'data': [{u'count': 5, u'name': u'Equity Research', u'value': u'2112'}, {u'count': 5, u'name': u'Equities', u'value': u'462'}, {u'count': 5, u'name': u'Portfolio Management', u'value': u'480'}, {u'count': 4, u'name': u'Financial Markets', u'value': u'1371'}, {u'count': 4, u'name': u'Derivatives', u'value': u'814'}, {u'count': 3, u'name': u'Mutual Funds', u'value': u'1043'}, {u'count': 3, u'name': u'Financial Modeling', u'value': u'1245'}, {u'count': 3, u'name': u'MIS', u'value': u'1303'}, {u'count': 3, u'name': u'Negotiation', u'value': u'256'}, {u'count': 3, u'name': u'Trading', u'value': u'283'}], u'i18n_skill_explicit': u'Skills'}}, u'i18n_not_allow_conn_browse': u'xyz does not allow connection browsing.', u'lix_deferLoad': u'B', u'i18n_count_string': u'500+', u'lix_deferOnload': u'B', u'i18n_S_NETWORK': u"xyz's Network"}}
也许你想加载原始字符串开头。只需一次即可:
>>> test = json.loads('{"content":{"search_highlight":{},"message_exchanged":{"messagesOnlyToViewee":true,"messagesOnlyToViewer":true},"Certifications":{"certsMpr":{},"empty":{}},"lix_treasury_callout":"B","network_overview":{"lix_deferLoad":"B","lix_showDetail":"control","distance":3,"lix_deferOnload":"B","allow_pivot_search":false,"i18n_S_NETWORK":"xyz\'s Network","facets":{"skill_explicit":{"data":[{"count":5,"name":"Equity Research","value":"2112"},{"count":5,"name":"Equities","value":"462"},{"count":5,"name":"Portfolio Management","value":"480"},{"count":4,"name":"Financial Markets","value":"1371"},{"count":4,"name":"Derivatives","value":"814"},{"count":3,"name":"Mutual Funds","value":"1043"},{"count":3,"name":"Financial Modeling","value":"1245"},{"count":3,"name":"MIS","value":"1303"},{"count":3,"name":"Negotiation","value":"256"},{"count":3,"name":"Trading","value":"283"}],"i18n_skill_explicit":"Skills"},"geo_region":{"data":[{"count":39,"name":"India","value":"in:0"},{"count":30,"name":"Mumbai Area, India","value":"in:7150"},{"count":1,"name":"Hyderabad Area, India","value":"in:6508"},{"count":1,"name":"Chandigarh Area, India","value":"in:6879"},{"count":1,"name":"Kolkata Area, India","value":"in:7003"},{"count":1,"name":"Moradabad Area, India","value":"in:7046"},{"count":1,"name":"Lucknow Area, India","value":"in:7093"},{"count":1,"name":"Bengaluru Area, India","value":"in:7127"},{"count":1,"name":"New Delhi Area, India","value":"in:7151"},{"count":1,"name":"Jaipur Area, India","value":"in:7287"}],"i18n_geo_region":"Location"},"current_company":{"data":[{"count":5,"name":"L&T Mutual Fund","value":"919608"},{"count":2,"name":"IDFC","value":"21175"},{"count":1,"name":"Indiabulls","value":"10359"},{"count":1,"name":"Innodata Inc.","value":"10827"},{"count":1,"name":"Tangoe","value":"11341"},{"count":1,"name":"Philippine National Bank","value":"17277"},{"count":1,"name":"bc","value":"164575"},{"count":1,"name":"Metropolis Health Services (I) Ltd","value":"236316"},{"count":1,"name":"India Infoline Ltd","value":"239538"},{"count":1,"name":"Wadhwa Group","value":"312891"}],"i18n_current_company":"Company"},"education":{"i18n_education":"School","data":[{"count":8,"name":"University of Mumbai","value":"13663"},{"count":1,"name":"Anna University","value":"13412"},{"count":1,"name":"Jawaharlal Nehru Technological University","value":"13520"},{"count":1,"name":"Karnataka State Open University","value":"13530"},{"count":1,"name":"University of Allahabad","value":"13652"},{"count":1,"name":"University of Lucknow","value":"13661"},{"count":1,"name":"West Bengal University of Technology","value":"13686"},{"count":1,"name":"University of Calcutta","value":"20302"},{"count":1,"name":"Welingkar Institute of Management","value":"21296"},{"count":1,"name":"The Institute of Chartered Accountants of India","value":"22460"}]},"industry":{"data":[{"count":22,"name":"Financial Services","value":"43"},{"count":4,"name":"Human Resources","value":"137"},{"count":2,"name":"Real Estate","value":"44"},{"count":2,"name":"Information Technology and Services","value":"96"},{"count":2,"name":"Capital Markets","value":"129"},{"count":1,"name":"Telecommunications","value":"8"},{"count":1,"name":"Hospital & Health Care","value":"14"},{"count":1,"name":"Banking","value":"41"},{"count":1,"name":"Investment Management","value":"46"},{"count":1,"name":"Education Management","value":"69"}],"i18n_industry":"Industry"}},"i18n_not_allow_conn_browse":"xyz does not allow connection browsing.","link__overview_detail":"/profile/profile\\u002dv2\\u002dnetwork\\u002doverview\\u002ddetail?id=131506997&srchFacet=search_facet_param&srchFacetValue=search_facet_value_param&facetResultCount=search_facet_count_param","i18n_count_string":"500+"}}}')
>>> test['content']
{u'message_exchanged': {u'messagesOnlyToViewee': True, u'messagesOnlyToViewer': True}, u'search_highlight': {}, u'lix_treasury_callout': u'B', u'Certifications': {u'certsMpr': {}, u'empty': {}}, u'network_overview': {u'lix_showDetail': u'control', u'distance': 3, u'link__overview_detail': u'/profile/profile-v2-network-overview-detail?id=131506997&srchFacet=search_facet_param&srchFacetValue=search_facet_value_param&facetResultCount=search_facet_count_param', u'allow_pivot_search': False, u'facets': {u'current_company': {u'i18n_current_company': u'Company', u'data': [{u'count': 5, u'name': u'L&T Mutual Fund', u'value': u'919608'}, {u'count': 2, u'name': u'IDFC', u'value': u'21175'}, {u'count': 1, u'name': u'Indiabulls', u'value': u'10359'}, {u'count': 1, u'name': u'Innodata Inc.', u'value': u'10827'}, {u'count': 1, u'name': u'Tangoe', u'value': u'11341'}, {u'count': 1, u'name': u'Philippine National Bank', u'value': u'17277'}, {u'count': 1, u'name': u'bc', u'value': u'164575'}, {u'count': 1, u'name': u'Metropolis Health Services (I) Ltd', u'value': u'236316'}, {u'count': 1, u'name': u'India Infoline Ltd', u'value': u'239538'}, {u'count': 1, u'name': u'Wadhwa Group', u'value': u'312891'}]}, u'geo_region': {u'data': [{u'count': 39, u'name': u'India', u'value': u'in:0'}, {u'count': 30, u'name': u'Mumbai Area, India', u'value': u'in:7150'}, {u'count': 1, u'name': u'Hyderabad Area, India', u'value': u'in:6508'}, {u'count': 1, u'name': u'Chandigarh Area, India', u'value': u'in:6879'}, {u'count': 1, u'name': u'Kolkata Area, India', u'value': u'in:7003'}, {u'count': 1, u'name': u'Moradabad Area, India', u'value': u'in:7046'}, {u'count': 1, u'name': u'Lucknow Area, India', u'value': u'in:7093'}, {u'count': 1, u'name': u'Bengaluru Area, India', u'value': u'in:7127'}, {u'count': 1, u'name': u'New Delhi Area, India', u'value': u'in:7151'}, {u'count': 1, u'name': u'Jaipur Area, India', u'value': u'in:7287'}], u'i18n_geo_region': u'Location'}, u'industry': {u'i18n_industry': u'Industry', u'data': [{u'count': 22, u'name': u'Financial Services', u'value': u'43'}, {u'count': 4, u'name': u'Human Resources', u'value': u'137'}, {u'count': 2, u'name': u'Real Estate', u'value': u'44'}, {u'count': 2, u'name': u'Information Technology and Services', u'value': u'96'}, {u'count': 2, u'name': u'Capital Markets', u'value': u'129'}, {u'count': 1, u'name': u'Telecommunications', u'value': u'8'}, {u'count': 1, u'name': u'Hospital & Health Care', u'value': u'14'}, {u'count': 1, u'name': u'Banking', u'value': u'41'}, {u'count': 1, u'name': u'Investment Management', u'value': u'46'}, {u'count': 1, u'name': u'Education Management', u'value': u'69'}]}, u'education': {u'data': [{u'count': 8, u'name': u'University of Mumbai', u'value': u'13663'}, {u'count': 1, u'name': u'Anna University', u'value': u'13412'}, {u'count': 1, u'name': u'Jawaharlal Nehru Technological University', u'value': u'13520'}, {u'count': 1, u'name': u'Karnataka State Open University', u'value': u'13530'}, {u'count': 1, u'name': u'University of Allahabad', u'value': u'13652'}, {u'count': 1, u'name': u'University of Lucknow', u'value': u'13661'}, {u'count': 1, u'name': u'West Bengal University of Technology', u'value': u'13686'}, {u'count': 1, u'name': u'University of Calcutta', u'value': u'20302'}, {u'count': 1, u'name': u'Welingkar Institute of Management', u'value': u'21296'}, {u'count': 1, u'name': u'The Institute of Chartered Accountants of India', u'value': u'22460'}], u'i18n_education': u'School'}, u'skill_explicit': {u'data': [{u'count': 5, u'name': u'Equity Research', u'value': u'2112'}, {u'count': 5, u'name': u'Equities', u'value': u'462'}, {u'count': 5, u'name': u'Portfolio Management', u'value': u'480'}, {u'count': 4, u'name': u'Financial Markets', u'value': u'1371'}, {u'count': 4, u'name': u'Derivatives', u'value': u'814'}, {u'count': 3, u'name': u'Mutual Funds', u'value': u'1043'}, {u'count': 3, u'name': u'Financial Modeling', u'value': u'1245'}, {u'count': 3, u'name': u'MIS', u'value': u'1303'}, {u'count': 3, u'name': u'Negotiation', u'value': u'256'}, {u'count': 3, u'name': u'Trading', u'value': u'283'}], u'i18n_skill_explicit': u'Skills'}}, u'i18n_not_allow_conn_browse': u'xyz does not allow connection browsing.', u'lix_deferLoad': u'B', u'i18n_count_string': u'500+', u'lix_deferOnload': u'B', u'i18n_S_NETWORK': u"xyz's Network"}}
答案 1 :(得分:0)
你抛弃了太多:
test1 = json.dumps(test)
转储转储的字符串转储......您应该直接调用test2 = json.loads('{"content...)
。
(如果你真的想要这个trpile-dumping出于任何原因,你需要test2 = json.loads(json.loads(json.loads(test1)))
)