0

Apex Code:

  List<order_batch__c> bcList = new List<order_batch__c>();
       bcList= [select Or_invoiceadd1__c,Or_invoiceadd2__c,Or_invoiceadd3__c,Or_invoiceadd4__c from order_batch__c  where  Quote_Line_Item__r.Quote1__c= : Apexpages.currentPage().getParameters().get('Id') order by Order_numbering__c]; 
      String JSONString = '';
     for (order_batch__c order : bcList) {
     String orderJSON = '{\n' +
    ' "Or_invoiceadd1__c" :"' + order.Or_invoiceadd1__c + '",\n' +
    '},';
      JSONString +=orderJSON ;
             system.debug( JSONString);
         } 

DEBUG :Or_invoiceadd1__c:null

I am sending json string through http post when the filed data was empty it was returning null i dont want to return it as null .i want to return as blank only .how can i do that one .

  • 1
    Why don't you just serialize bcList instead of manually building your JSON string? e.g. json.serializePretty(bcList);? – Phil Hawthorn Sep 17 '14 at 10:10
  • 1
    Getting 'null' is the default toString behavoir of objects and fields. It's good to know that. – Samuel De Rycke Sep 17 '14 at 10:22
2

If you want to continue to build a JSON string in the way that you are (rather than using json.serialize) then you'll have to do something like this:

' "Or_invoiceadd1__c" :"' + (order.Or_invoiceadd1__c == null ? '' : order.Or_invoiceadd1__c) + '",\n' +
|improve this answer|||||
  • json.serialize i want column in particular order but it is not coming with json.serialize – Teja Sep 17 '14 at 10:27
  • Still i am receiving null for the above code – Teja Sep 17 '14 at 10:28
  • My bad, wrap it in () as per updated example – Phil Hawthorn Sep 17 '14 at 10:30
  • Perfect answer. – Teja Sep 17 '14 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.