Доброго времени суток.
Особо не имел дело с json parser на СФ, только xml
Достаю обычные данные вот так:
JSONParser parser = JSON.createParser(jsonStr);
String TokenJson;
while (parser.nextToken() != null)
{
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == FindTeg))
{
parser.nextToken();
TokenJson = parser.getText();
}
}
Доброго времени суток. Особо не имел дело с json parser на СФ, только xml Достаю обычные данные вот так: [code] JSONParser parser = JSON.createParser(jsonStr); String TokenJson; while (parser.nextToken() != null) { if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == FindTeg)) { parser.nextToken(); TokenJson = parser.getText(); } } [/code] Но сейчас FindTeg содержит вложенный лист элементов. И мне надо достать один элемент из этого листа по имени. Доки содержат интересный, но более сложный подход. А хотелось бы просто(такое вот пожелание с выше). Прошу помощи.
[quote="DevNull"]Но сейчас FindTeg содержит вложенный лист элементов. [/quote] Что-то по твоему коду не понятно как FindTeg может содержать лист чего-то там, если ты его используешь вот так parser.getText() == FindTeg. По логике в FindTeg лежит текст. Для полноты картины напиши пример исходного json и того что ты хочешь получить из него.
Может лучше посмотреть в сторону Json Serialization and Deserialization? Если структура json постоянная то может стоит ее десериализировать в inner object и работать как с обычным объектом?
Может лучше посмотреть в сторону Json Serialization and Deserialization? Если структура json постоянная то может стоит ее десериализировать в inner object и работать как с обычным объектом?
{
"Items"
:
[{
"ReportID": "83807275D41A47F889EB",
"ReportOwnerID": "WebAdmin@mackenziefinancial.com",
"ExpenseTypeCode": "AIRFR",
"ExpenseTypeName": "Air",
"SpendCategoryCode": "AIRFR",
"SpendCategoryName": "Airfare",
"PaymentTypeID": "gWpeYjBcS7yOxoeO9CU$s5wJ9RlpKC",
"PaymentTypeName": "Cash",
"TransactionDate": "2015-02-19T00:00:00",
"TransactionCurrencyCode": "CAD",
"TransactionAmount": 123.00000000,
"ExchangeRate": 1.00000000000000,
"PostedAmount": 123.00000000,
"ApprovedAmount": 123.00000000,
"VendorDescription": "234",
"VendorListItemID": null,
"VendorListItemName": null,
"LocationID": "gWuF4OKqoXi8hIi70KwEVK7ksgEl$p1sc3vw",
"LocationName": "Toronto, Ontario",
"LocationSubdivision": "Ontario",
"LocationCountry": "CA",
"Description": "ert",
"IsPersonal": false,
"IsBillable": false,
"IsPersonalCardCharge": false,
"HasImage": false,
"IsImageRequired": true,
"ReceiptReceived": false,
"TaxReceiptType": "R",
"ElectronicReceiptID": null,
"CompanyCardTransactionID": null,
"TripID": null,
"HasItemizations": false,
"AllocationType": "N",
"HasAttendees": false,
"HasVAT": false,
"HasAppliedCashAdvance": false,
"HasComments": false,
"HasExceptions": false,
"IsPaidByExpensePay": false,
"EmployeeBankAccountID": null,
"Journey": null,
"LastModified": "2015-02-19T12:23:33.423",
"FormID": "gWsfxtb$pR0LU8ysQf5ZlvFoA9Hl0BWiqhKg",
"OrgUnit1": {
"Type": "ConnectedList",
"Value": "Distribution",
"Code": "7140",
"ListItemID": "gWrx1fO1OWKtaiIo9NlMUvQmWS$svyDSSGIA"
},
"OrgUnit2": {
"Type": "ConnectedList",
"Value": "Sales Operations",
"Code": "7300",
"ListItemID": "gWrx1fO1OWKFTYf2JkleuofY9DTr7p$pyURg"
},
"OrgUnit3": null,
"OrgUnit4": null,
"OrgUnit5": null,
"OrgUnit6": null,
"Custom1": null,
"Custom2": null,
"Custom3": null,
"Custom4": null,
"Custom5": null,
"Custom6": null,
"Custom7": null,
"Custom8": null,
"Custom9": null,
"Custom10": null,
"Custom11": null,
"Custom12": null,
"Custom13": null,
"Custom14": null,
"Custom15": null,
"Custom16": null,
"Custom17": null,
"Custom18": null,
"Custom19": null,
"Custom20": null,
"Custom21": null,
"Custom22": null,
"Custom23": null,
"Custom24": null,
"Custom25": null,
"Custom26": null,
"Custom27": null,
"Custom28": null,
"Custom29": null,
"Custom30": null,
"Custom31": null,
"Custom32": null,
"Custom33": null,
"Custom34": null,
"Custom35": null,
"Custom36": null,
"Custom37": null,
"Custom38": null,
"Custom39": null,
"Custom40": null,
"ID": "gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA",
"URI": "https://implementation.concursolutions.com/api/v3.0/expense/entries/gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA"
}], "NextPage"
:
null
}
[code] { "Items" : [{ "ReportID": "83807275D41A47F889EB", "ReportOwnerID": "WebAdmin@mackenziefinancial.com", "ExpenseTypeCode": "AIRFR", "ExpenseTypeName": "Air", "SpendCategoryCode": "AIRFR", "SpendCategoryName": "Airfare", "PaymentTypeID": "gWpeYjBcS7yOxoeO9CU$s5wJ9RlpKC", "PaymentTypeName": "Cash", "TransactionDate": "2015-02-19T00:00:00", "TransactionCurrencyCode": "CAD", "TransactionAmount": 123.00000000, "ExchangeRate": 1.00000000000000, "PostedAmount": 123.00000000, "ApprovedAmount": 123.00000000, "VendorDescription": "234", "VendorListItemID": null, "VendorListItemName": null, "LocationID": "gWuF4OKqoXi8hIi70KwEVK7ksgEl$p1sc3vw", "LocationName": "Toronto, Ontario", "LocationSubdivision": "Ontario", "LocationCountry": "CA", "Description": "ert", "IsPersonal": false, "IsBillable": false, "IsPersonalCardCharge": false, "HasImage": false, "IsImageRequired": true, "ReceiptReceived": false, "TaxReceiptType": "R", "ElectronicReceiptID": null, "CompanyCardTransactionID": null, "TripID": null, "HasItemizations": false, "AllocationType": "N", "HasAttendees": false, "HasVAT": false, "HasAppliedCashAdvance": false, "HasComments": false, "HasExceptions": false, "IsPaidByExpensePay": false, "EmployeeBankAccountID": null, "Journey": null, "LastModified": "2015-02-19T12:23:33.423", "FormID": "gWsfxtb$pR0LU8ysQf5ZlvFoA9Hl0BWiqhKg", "OrgUnit1": { "Type": "ConnectedList", "Value": "Distribution", "Code": "7140", "ListItemID": "gWrx1fO1OWKtaiIo9NlMUvQmWS$svyDSSGIA" }, "OrgUnit2": { "Type": "ConnectedList", "Value": "Sales Operations", "Code": "7300", "ListItemID": "gWrx1fO1OWKFTYf2JkleuofY9DTr7p$pyURg" }, "OrgUnit3": null, "OrgUnit4": null, "OrgUnit5": null, "OrgUnit6": null, "Custom1": null, "Custom2": null, "Custom3": null, "Custom4": null, "Custom5": null, "Custom6": null, "Custom7": null, "Custom8": null, "Custom9": null, "Custom10": null, "Custom11": null, "Custom12": null, "Custom13": null, "Custom14": null, "Custom15": null, "Custom16": null, "Custom17": null, "Custom18": null, "Custom19": null, "Custom20": null, "Custom21": null, "Custom22": null, "Custom23": null, "Custom24": null, "Custom25": null, "Custom26": null, "Custom27": null, "Custom28": null, "Custom29": null, "Custom30": null, "Custom31": null, "Custom32": null, "Custom33": null, "Custom34": null, "Custom35": null, "Custom36": null, "Custom37": null, "Custom38": null, "Custom39": null, "Custom40": null, "ID": "gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA", "URI": "https://implementation.concursolutions.com/api/v3.0/expense/entries/gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA" }], "NextPage" : null } [/code] мне надо получить значение OrgUnit1.Code
В мапу десериализовать http://salesforce.stackexchange.com/questions/7523/what-is-the-correct-json-to-deserialize-a-map-of-sobjects
Не совсем понимаю как применить. Буду благодарен если подскажете на моем примере.
Не совсем понимаю как применить. Буду благодарен если подскажете на моем примере.
String jsonStr = '{"Items":[{"ReportID":"83807275D41A47F889EB","ReportOwnerID":"WebAdmin@mackenziefinancial.com","ExpenseTypeCode":"AIRFR","ExpenseTypeName":"Air","SpendCategoryCode":"AIRFR","SpendCategoryName":"Airfare","PaymentTypeID":"gWpeYjBcS7yOxoeO9CU$s5wJ9RlpKC","PaymentTypeName":"Cash","TransactionDate":"2015-02-19T00:00:00","TransactionCurrencyCode":"CAD","TransactionAmount":123.00000000,"ExchangeRate":1.00000000000000,"PostedAmount":123.00000000,"ApprovedAmount":123.00000000,"VendorDescription":"234","VendorListItemID":null,"VendorListItemName":null,"LocationID":"gWuF4OKqoXi8hIi70KwEVK7ksgEl$p1sc3vw","LocationName":"Toronto, Ontario","LocationSubdivision":"Ontario","LocationCountry":"CA","Description":"ert","IsPersonal":false,"IsBillable":false,"IsPersonalCardCharge":false,"HasImage":false,"IsImageRequired":true,"ReceiptReceived":false,"TaxReceiptType":"R","ElectronicReceiptID":null,"CompanyCardTransactionID":null,"TripID":null,"HasItemizations":false,"AllocationType":"N","HasAttendees":false,"HasVAT":false,"HasAppliedCashAdvance":false,"HasComments":false,"HasExceptions":false,"IsPaidByExpensePay":false,"EmployeeBankAccountID":null,"Journey":null,"LastModified":"2015-02-19T12:23:33.423","FormID":"gWsfxtb$pR0LU8ysQf5ZlvFoA9Hl0BWiqhKg","OrgUnit1":{"Type":"ConnectedList","Value":"Distribution","Code":"7140","ListItemID":"gWrx1fO1OWKtaiIo9NlMUvQmWS$svyDSSGIA"},"OrgUnit2":{"Type":"ConnectedList","Value":"Sales Operations","Code":"7300","ListItemID":"gWrx1fO1OWKFTYf2JkleuofY9DTr7p$pyURg"},"OrgUnit3":null,"OrgUnit4":null,"OrgUnit5":null,"OrgUnit6":null,"Custom1":null,"Custom2":null,"Custom3":null,"Custom4":null,"Custom5":null,"Custom6":null,"Custom7":null,"Custom8":null,"Custom9":null,"Custom10":null,"Custom11":null,"Custom12":null,"Custom13":null,"Custom14":null,"Custom15":null,"Custom16":null,"Custom17":null,"Custom18":null,"Custom19":null,"Custom20":null,"Custom21":null,"Custom22":null,"Custom23":null,"Custom24":null,"Custom25":null,"Custom26":null,"Custom27":null,"Custom28":null,"Custom29":null,"Custom30":null,"Custom31":null,"Custom32":null,"Custom33":null,"Custom34":null,"Custom35":null,"Custom36":null,"Custom37":null,"Custom38":null,"Custom39":null,"Custom40":null,"ID":"gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA","URI":"https://implementation.concursolutions.com/api/v3.0/expense/entries/gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA"}],"NextPage":null}';Map<String, Object> mapJsonObjects = (Map<String, Object>) JSON.deserializeUntyped(jsonStr);
List<Object> items = (List<Object>)mapJsonObjects.get('Items');for(Object item : items){
Map<String, Object> itemMap = (Map<String, Object>)item;
Map<String, Object> OrgUnit1 = (Map<String, Object>)itemMap.get('OrgUnit1');
object code = OrgUnit1.get('Code');
System.debug('OrgUnit1.code: ' + code);
}
однако баг на форуме нашёлся :o
[code] String jsonStr = '{"Items":[{"ReportID":"83807275D41A47F889EB","ReportOwnerID":"WebAdmin@mackenziefinancial.com","ExpenseTypeCode":"AIRFR","ExpenseTypeName":"Air","SpendCategoryCode":"AIRFR","SpendCategoryName":"Airfare","PaymentTypeID":"gWpeYjBcS7yOxoeO9CU$s5wJ9RlpKC","PaymentTypeName":"Cash","TransactionDate":"2015-02-19T00:00:00","TransactionCurrencyCode":"CAD","TransactionAmount":123.00000000,"ExchangeRate":1.00000000000000,"PostedAmount":123.00000000,"ApprovedAmount":123.00000000,"VendorDescription":"234","VendorListItemID":null,"VendorListItemName":null,"LocationID":"gWuF4OKqoXi8hIi70KwEVK7ksgEl$p1sc3vw","LocationName":"Toronto, Ontario","LocationSubdivision":"Ontario","LocationCountry":"CA","Description":"ert","IsPersonal":false,"IsBillable":false,"IsPersonalCardCharge":false,"HasImage":false,"IsImageRequired":true,"ReceiptReceived":false,"TaxReceiptType":"R","ElectronicReceiptID":null,"CompanyCardTransactionID":null,"TripID":null,"HasItemizations":false,"AllocationType":"N","HasAttendees":false,"HasVAT":false,"HasAppliedCashAdvance":false,"HasComments":false,"HasExceptions":false,"IsPaidByExpensePay":false,"EmployeeBankAccountID":null,"Journey":null,"LastModified":"2015-02-19T12:23:33.423","FormID":"gWsfxtb$pR0LU8ysQf5ZlvFoA9Hl0BWiqhKg","OrgUnit1":{"Type":"ConnectedList","Value":"Distribution","Code":"7140","ListItemID":"gWrx1fO1OWKtaiIo9NlMUvQmWS$svyDSSGIA"},"OrgUnit2":{"Type":"ConnectedList","Value":"Sales Operations","Code":"7300","ListItemID":"gWrx1fO1OWKFTYf2JkleuofY9DTr7p$pyURg"},"OrgUnit3":null,"OrgUnit4":null,"OrgUnit5":null,"OrgUnit6":null,"Custom1":null,"Custom2":null,"Custom3":null,"Custom4":null,"Custom5":null,"Custom6":null,"Custom7":null,"Custom8":null,"Custom9":null,"Custom10":null,"Custom11":null,"Custom12":null,"Custom13":null,"Custom14":null,"Custom15":null,"Custom16":null,"Custom17":null,"Custom18":null,"Custom19":null,"Custom20":null,"Custom21":null,"Custom22":null,"Custom23":null,"Custom24":null,"Custom25":null,"Custom26":null,"Custom27":null,"Custom28":null,"Custom29":null,"Custom30":null,"Custom31":null,"Custom32":null,"Custom33":null,"Custom34":null,"Custom35":null,"Custom36":null,"Custom37":null,"Custom38":null,"Custom39":null,"Custom40":null,"ID":"gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA","URI":"https://implementation.concursolutions.com/api/v3.0/expense/entries/gWvWckcrsgo2vYjzYZhJK93RMdwEDbOAuTA"}],"NextPage":null}'; Map<String, Object> mapJsonObjects = (Map<String, Object>) JSON.deserializeUntyped(jsonStr); List<Object> items = (List<Object>)mapJsonObjects.get('Items'); for(Object item : items){ Map<String, Object> itemMap = (Map<String, Object>)item; Map<String, Object> OrgUnit1 = (Map<String, Object>)itemMap.get('OrgUnit1'); object code = OrgUnit1.get('Code'); System.debug('OrgUnit1.code: ' + code); } [/code] однако баг на форуме нашёлся :o
Это не баг, это фича :)))))))))))))))))))))))
Починю :)))))))))
Это не баг, это фича :))))))))))))))))))))))) Починю :)))))))))
[quote="Дима Лисовский"]hotfix http://prntscr.com/69mwxi :)[/quote] Да да, знаю :))) Спасибо.
Помогите плиз. Мозг так уже забит, вообще не вкуриваю ничвото(
Помогите плиз. Мозг так уже забит, вообще не вкуриваю ничвото(
я же всё сделал, что не понятно?
[quote="DevNull"]Помогите плиз. Мозг так уже забит, вообще не вкуриваю ничвото([/quote] я же всё сделал, что не понятно?
О! Я ж говорю что глючит меня уже) Сорри. Дедлайн тут на одном проекте, мозг спалил уже. Спасибо!!
О! Я ж говорю что глючит меня уже) Сорри. Дедлайн тут на одном проекте, мозг спалил уже. Спасибо!!