最高:分配没有键的地图到列表

发布于 2025-01-24 21:46:38 字数 738 浏览 1 评论 0原文

对不起,新手问题。在这里学习顶点。一直在研究一个问题几个小时,似乎无法将其淘汰。

我有一个我需要转换为列表的JSON ...通过API检索JSON。代码非常简单,它只是几行。

JSON看起来像这样: {“ ID”:1,“缩写”:“ ATL”,“ City”:“ Atlanta”,“会议”:“ East”,“ division”,“ Southeast”,“ full_name”:“ Atlanta Hawks” ,“名称”:“ Hawks”}

和我被告知要检索的代码看起来像这样:

map< string,object> resuctmap =(map< string,object>)json.deserializeuntyped(results.getBody());

基于提供的JSON,似乎没有分配的地图键,所以我不知道如何获取它,以便我可以将其分配给列表...

我已经尝试将其直接分配给列表,但是在那里我也没有取得太大的成功。

我已经尝试过:

list< object>其他=(list< object>)result.get(''');

我也尝试了以下方法: list< object>其他=(list< object>)results.keyset()[0];

我敢肯定这很简单。任何帮助将不胜感激。 谢谢

Sorry for the newbie question. Learning Apex here. Been working on an issue for several hours and can't seem to needle it out.

I have a JSON that I need converted to a List... the JSON is retrieved via an API. The code is pretty simple, it is only a couple of lines.

The JSON looks like this:
{"id":1,"abbreviation":"ATL","city":"Atlanta","conference":"East","division":"Southeast","full_name":"Atlanta Hawks","name":"Hawks"}

And the code I am told to use to retrieve it looks like this:

Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(results.getBody());

Based on the JSON provided, there does not appear to be a MAP key being assigned, so I have no idea how to get it so that I may assign it to a List...

I've already tried assigning it directly to a List, but I didn't get much success there either.

I've tried this already:

List<Object> other = (List<Object>) results.get('');

I've also tried this:
List<Object> other = (List<Object>) results.keySet()[0];

I'm sure it is something simple. Any help would be appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2025-01-31 21:46:38

您无法将地图转换为没有ID的列表。或者为什么要将其转换为列表。使用地图本身。以下是如何使用JSON响应使用它的代码示例。例如,我想将此JSON响应插入到联系人记录中,因此我可以使用地图本身对其进行映射。:

@restresource(urlmapping ='/uriid/*')
全球班级ownericrm {

@HttpPut
global static String UpdateOwnerinfo(){
    RestRequest req= RestContext.request;
    RestResponse res= RestContext.response;
    res.addHeader('Content-type','application/JSON');
    Map<String,Object> responseresult=new Map<String,Object>();
    Map<String,Object> results= (Map<String,Object>)JSON.deserializeUntyped(req.requestBody.toString());
     List<contact> insertList = new List<contact>();
    
    for (String key : results.keySet()){
    System.debug(results.get(key));
    contact c= new contact();
    c.ContactField__C = results.get(id);
    c.ContactField1__C = results.get(city);
    c.ContactField2__C = results.get(conference);
    c.ContactField3__C = results.get(division);
    c.ContactField3__C = results.get(full_name);
    c.ContactField3__C = results.get(name);
    insertList.add(c);
    
    }
    if(!insertList.Isempty()){
        update insertList;
    }

You cant convert the map to a list without an id. Alternatively why to convert it to List. Use the map itself. Below is a code example of how you can use it using your JSON response. For example, I want to insert this JSON response into the contact record, so I can map it using the MAP itself.:

@RestResource(urlMapping='/URIId/*')
global class OwneriCRM {

@HttpPut
global static String UpdateOwnerinfo(){
    RestRequest req= RestContext.request;
    RestResponse res= RestContext.response;
    res.addHeader('Content-type','application/JSON');
    Map<String,Object> responseresult=new Map<String,Object>();
    Map<String,Object> results= (Map<String,Object>)JSON.deserializeUntyped(req.requestBody.toString());
     List<contact> insertList = new List<contact>();
    
    for (String key : results.keySet()){
    System.debug(results.get(key));
    contact c= new contact();
    c.ContactField__C = results.get(id);
    c.ContactField1__C = results.get(city);
    c.ContactField2__C = results.get(conference);
    c.ContactField3__C = results.get(division);
    c.ContactField3__C = results.get(full_name);
    c.ContactField3__C = results.get(name);
    insertList.add(c);
    
    }
    if(!insertList.Isempty()){
        update insertList;
    }
千里故人稀 2025-01-31 21:46:38

问题可以被忽略。不确定是否有办法撤回问题。

问题是基于以下格式发送以下API的事实:

{“ data”:{“更多内容”:“ supper”}}

,地图键是带有列表的'data'。在这种情况下,整个API是一个列表,并将地图的钥匙设置为实际值。

Question can be disregarded. Not sure if there is a way to withdraw the question.

Question was based on the fact that previous APIs had been sent in the following format:

{"data": {"more stuff": "stuff"} }

And the map key was 'data' with a list. In this scenario, the whole API was a list and the keys to the map were set in place with the actual values instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文