为地图生成json格式

时间:2018-08-09 11:06:09

标签: java json salesforce apex-code apex

我正在将地图传递给POST请求的Json主体,但格式不正确。

下面是我的代码:

Map<String,Double> Last12MSalesTransaction_c = new Map<String,Double>();
    Last12MSalesTransaction_c.put('1',l.Booking_Details_in_rupees_1M__c);
    Last12MSalesTransaction_c.put('2',l.Booking_Details_in_rupees_2M__c);
    Last12MSalesTransaction_c.put('3',l.Booking_Details_in_rupees_3M__c);
    Last12MSalesTransaction_c.put('4',l.Booking_Details_in_rupees_4M__c);
    Last12MSalesTransaction_c.put('5',l.Booking_Details_in_rupees_5M__c);
    Last12MSalesTransaction_c.put('6',l.Booking_Details_in_rupees_6M__c);

    string jsonstring = JSON.serialize(Last12MSalesTransaction_c);

    String body='{'+
        '"LeadSource":"'+ls.name+'",'+
        '"LoanAmount":'+l.Amount_in_Rs__c+','+
        '"Off_Stability":'+l.Office_Stability_in_Months__c+','+
        '"Opportunity_Record_Type":"'+l.Business_Type__c+'",'+
        '"PAN_ID":"'+l.PAN__c+'",'+
        '"Program_Type":"'+l.Program_Type__c+'",'+
        '"P1":'+l.Purchase_Price__c+','+
        '"Last12MSalesTransaction":"'+jsonstring+'"'+                         
        '}';

这是生成的json:

Body{"LeadSource":"UltraCash","LoanAmount":0,"Off_Stability":0,"Opportunity_Record_Type":"Consumer Loan","PAN_ID":"AOJPM2131F","Program_Type":"null","P1":0,"Last12MSalesTransaction":"{"12":21.0,"11":20.0,"10":19.0,"9":18.0,"8":17.0,"7":16.0,"6":15.0,"5":14.0,"4":13.0,"3":12.0,"2":11.0,"1":10.0}"}

但是我想要以下格式的json:

{
  "LeadSource": "UltraCash",
  "LoanAmount": 0,
  "Off_Stability": 0,
  "Opportunity_Record_Type": "Consumer Loan",
  "PAN_ID": "AOJPM2131F",
  "Program_Type": "null",
  "P1": 0,
  "Last12MSalesTransaction": [
    {
       "key" : "1", "value" : 10
    },
    {
       "key" : "2", "value" : 15
    },
    {
       "key" : "3", "value" : 10
    },
    {
       "key" : "4", "value" : 10
    },
    {
       "key" : "5", "value" : 20
    },
    {
       "key" : "6", "value" : 30
    }
  ]
}

请建议我一种获得预期结果的方法。

1 个答案:

答案 0 :(得分:0)

List<javafx.util.Pair<String, Double>> Last12MSalesTransaction = newArrayList(
                new javafx.util.Pair("1", 10),
                new javafx.util.Pair("2", 15),
                new javafx.util.Pair("3", 10)
        );

结果:

[ {
  "key" : "1",
  "value" : 10
}, {
  "key" : "2",
  "value" : 15
}, {
  "key" : "3",
  "value" : 10
} ]