0

I have this format to Read a Json and get the values on it:

public class Assets {
    public String accountNumber {get;set;}
    public Integer deposit {get;set;}
    public String description {get;set;}
    public String id {get;set;}
    public Integer interestRate  {get;set;}
    public String lastUpdated {get;set;}
    public List<String> owners {get;set;} //required
    public String recordType {get;set;}
    public String supplier {get;set;}
    public String type {get;set;} //required
    public String typeOfAccount {get;set;}
    public String typeOrigValue {get;set;}
    public Double value {get;set;}

    public Assets(JSONParser parser) {

        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'Type') {
                        type = parser.getText();
                    } else if (text == 'ID') {
                        id = parser.getText();
                    } else if (text == 'Owners') {
                        owners = arrayOfString(parser);
                    } else if (text == 'Value') {
                        value = Double.valueOf(parser.getText());
                    } else if (text == 'Description') {
                        description = parser.getText();
                    } else if (text == 'AccountNumber') {
                        accountNumber = parser.getText();
                    } else if (text == 'Supplier') {
                        supplier = parser.getText();
                    } else if (text == 'LastUpdated') {
                        lastUpdated = parser.getText();
                    } else if (text == 'RecordType') {
                        recordType = parser.getText();
                    } else if (text == 'TypeOfAccount') {
                        typeOfAccount = parser.getText();
                    } else if (text == 'TypeOrigValue') {
                        typeOrigValue = parser.getText();
                    } else if (text == 'InterestRate') {
                        interestRate = Integer.valueOf(parser.getText());
                    } else if (text == 'Deposit') {
                        deposit = Integer.valueOf(parser.getText());
                    } else {
                        System.debug(LoggingLevel.WARN, 'Assets consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

Know i want to put this values on a Class that gone to create a New Json someone can help me?

public class Tenas{
    public String Type {get;set;}
    public String Owners;
    public List<Cenas> Cenas= new List<Cenas>() ;
    public List<Menas> Menas= new List<Menas>();
    public string Supplier;

}

Class Assets already fetch the values of Json and i want yo put the values on Tenas class that will create a new JSON.

1
  • 2
    Right now, your question is not very clear. Please edit your question to include more details about what it is you're trying to accomplish, and where, precisely, you're stuck.
    – Derek F
    Jun 4, 2019 at 15:54

1 Answer 1

3

First, I would highly recommend avoiding using the parser. Instead use JSON.serialize and JSON.deserialize.

There are a number of questions on this site that talk about how to serialize and deserialize JSON. For instance this one.

In your case it would look like this:

public class Assets {
    public String accountNumber {get;set;}
    public Integer deposit {get;set;}
    public String description {get;set;}
    public String id {get;set;}
    public Integer interestRate  {get;set;}
    public String lastUpdated {get;set;}
    public List<String> owners {get;set;} //required
    public String recordType {get;set;}
    public String supplier {get;set;}
    public String type {get;set;} //required
    public String typeOfAccount {get;set;}
    public String typeOrigValue {get;set;}
    public Double value {get;set;}
}

Then you would do:

Http http = new Http();
HttpResponse response = http.send(request);
Assets assets = (Assets)JSON.deserialize(response.getBody(), assets.class);

Then to send out are request using the same class

Tenas tenas = new Tenas();

//... Do some work to fill up the tenas object

HttpRequest request = new HttpRequest();
request.setBody(JSON.serialize(tenas));
Http http = new Http();
http.send(request);
1
  • 2
    I think this line Assets assets = JSON.deserialize(response.getBody(), assets.class); needs to be (casting) Assets assets = (Assets) JSON.deserialize(response.getBody(), Assets.class);
    – cropredy
    Jun 4, 2019 at 16:05

Not the answer you're looking for? Browse other questions tagged .