与jackson对象到JSON转换问题

时间:2016-01-23 21:03:13

标签: java json object

import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.report.mongo.MakeMD5Checksum;
import com.report.mongo.MongoService;

import job.com.rpt.bootstrap.ApplicationConfigCache;
import service.com.rpt.db.MongoDataService;
import service.com.rpt.util.RPTUtil;

public class DTOtoJSONTest {

public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub

    Address studentAddress1 = new Address();
    studentAddress1.adrLine1 = "studentAddress1 adrLine1";
    studentAddress1.adrLine2 = "studentAddress1 adrLine2";
    studentAddress1.city = "studentAddress1 City";
    studentAddress1.postalCode = 1;

    Address studentAddress2 = new Address();
    studentAddress2.adrLine1 = "studentAddress2 adrLine1";
    studentAddress2.adrLine2 = "studentAddress2 adrLine2";
    studentAddress2.city = "studentAddress2 City";
    studentAddress2.postalCode = 2;

    StudentDTO studentDTO = new StudentDTO();
    studentDTO.name = "BK";
    studentDTO.roll = 1;
    studentDTO.age = 40;
    studentDTO.dateOfBirth = new Date();
    studentDTO.gender = "M";

    studentDTO.addresses = new Address[2];
    studentDTO.addresses[0] = studentAddress1;
    studentDTO.addresses[1] = studentAddress2;


    studentDTO.sportsInterests = new String[3];
    studentDTO.sportsInterests[0] = new String("Fooball");
    studentDTO.sportsInterests[1] = new String("VollyBall");
    studentDTO.sportsInterests[2] = new String("Carom");



    GurdianInfo father = new GurdianInfo();
    father.gurdianName = "Father Name";
    father.relation = "Father";
    father.address = new Address();
    father.address.adrLine1 = "Father addressline1";
    father.address.adrLine2 = "Father addressline2";
    father.address.city = "Father City";
    father.address.postalCode = 11;

    father.gurdianCntactInfo = new ArrayList<ContactInfo>();

    ContactInfo fContactInfo = new ContactInfo();
    fContactInfo.isPrimary = true;
    fContactInfo.cellNumber = "1234";
    fContactInfo.emailId = "";
    fContactInfo.type = "Cell";

    father.gurdianCntactInfo.add(fContactInfo);

    fContactInfo = new ContactInfo();
    fContactInfo.isPrimary = false;
    fContactInfo.cellNumber = "5678";
    fContactInfo.emailId = "";
    fContactInfo.type = "Cell";

    father.gurdianCntactInfo.add(fContactInfo);

    fContactInfo = new ContactInfo();
    fContactInfo.isPrimary = false;
    fContactInfo.cellNumber = "";
    fContactInfo.emailId = "father email";
    fContactInfo.type = "email";

    father.gurdianCntactInfo.add(fContactInfo);

    studentDTO.gurdianInfo = new ArrayList<GurdianInfo>();
    studentDTO.gurdianInfo.add(father);


    GurdianInfo mother = new GurdianInfo();
    mother.gurdianName = "Father Name";
    mother.relation = "Father";
    mother.address = new Address();
    mother.address.adrLine1 = "Father addressline1";
    mother.address.adrLine2 = "Father addressline2";
    mother.address.city = "Father City";
    mother.address.postalCode = 11;

    mother.gurdianCntactInfo = new ArrayList<ContactInfo>();

    ContactInfo mContactInfo = new ContactInfo();
    mContactInfo.isPrimary = true;
    mContactInfo.cellNumber = "PQRS";
    mContactInfo.emailId = "";
    mContactInfo.type = "Cell";

    mother.gurdianCntactInfo.add(mContactInfo);

    mContactInfo = new ContactInfo();
    mContactInfo.isPrimary = false;
    mContactInfo.cellNumber = "";
    mContactInfo.emailId = "mother email";
    mContactInfo.type = "email";

    mother.gurdianCntactInfo.add(mContactInfo);
    studentDTO.gurdianInfo.add(mother);

    CurriculamDetail curriculamDetail = new CurriculamDetail();
    curriculamDetail.curriculamName = "MCA";
    curriculamDetail.standard = "MASTERS";
    curriculamDetail.subjects = new String[3];
    curriculamDetail.subjects[0] = "SUB0";
    curriculamDetail.subjects[1] = "SUB1";
    curriculamDetail.subjects[2] = "SUB2";
    studentDTO.curriculamDetail = curriculamDetail;
    //Above part just creates an object
    // Main code starts below
    ObjectMapper mapperObj = new ObjectMapper();

    mapperObj.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    JSONObject studentJSON = new JSONObject();
        convertToJSON(studentDTO, studentJSON);
        System.out.println(studentJSON.toJSONString()); //This line prints {}
    }


public static void convertToJSON(Object object, JSONObject jsonObject) throws Exception{

    ObjectMapper mapperObj = new ObjectMapper();
    mapperObj.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

    String jsonStr = mapperObj.writeValueAsString(object); 
    ;
    JSONParser parser = new JSONParser();
    JSONObject local = (JSONObject)parser.parse(jsonStr);
    jsonObject = local;
        mapperObj.writeValueAsString(object);
    System.out.println("AFTER CONEVRSION ****************** " + jsonObject.toJSONString());  //This line prints JSON 

}

}

在上面的代码中,我尝试使用jackson将对象转换为JSON对象。该对象正在转换为JSON对象,但问题是JSON对象仅在convertToJSON方法中可用(它打印JSON完美)。我无法在main方法中打印JSON。在main方法中,它打印&#34; {}&#34;。请帮忙。

由于

0 个答案:

没有答案
相关问题