Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jackson的对象到JSON转换问题 导入java.io.File; 导入java.lang.reflect.Array; 导入java.lang.reflect.Field; 导入java.math.BigDecimal; 导入java.sql.Time; 导入java.sql.Timestamp; 导入java.util.ArrayList; 导入java.util.Date; 导入java.util.List; 导入org.apache.commons.lang3.StringUtils; 导入org.json.simple.JSONArray; 导入org.json.simple.JSONObject; 导入org.json.simple.parser.JSONParser; 导入com.fasterxml.jackson.annotation.jsonautodect.Visibility; 导入com.fasterxml.jackson.annotation.PropertyAccessor; 导入com.fasterxml.jackson.databind.ObjectMapper; 导入com.fasterxml.jackson.databind.ObjectWriter; 导入com.report.mongo.MakeMD5Checksum; 导入com.report.mongo.MongoService; 导入job.com.rpt.bootstrap.ApplicationConfigCache; import service.com.rpt.db.MongoDataService; import service.com.rpt.util.RPTUtil; 公共类DTOtoJSONTest{ 公共静态void main(字符串[]args)引发异常{ //TODO自动生成的方法存根 Address studentAddress1=新地址(); studentaddress 1.adrLine1=“studentaddress 1 adrLine1”; studentaddress 1.adrLine2=“studentaddress 1 adrLine2”; studentaddress 1.city=“studentaddress 1城市”; studentAddress1.postalCode=1; Address studentAddress2=新地址(); studentaddress 2.adrLine1=“studentaddress 2 adrLine1”; studentaddress 2.adrLine2=“studentaddress 2 adrLine2”; studentaddress 2.city=“studentaddress 2城市”; 学生地址2.postalCode=2; StudentDTO StudentDTO=新StudentDTO(); studentDTO.name=“BK”; studentDTO.roll=1; 学生年龄=40; studentDTO.dateOfBirth=新日期(); studentDTO.gender=“M”; studentDTO.addresses=新地址[2]; studentDTO.addresses[0]=studentAddress1; 学生地址[1]=学生地址2; studentDTO.sportsInterests=新字符串[3]; studentDTO.sportsInterests[0]=新字符串(“Fooball”); studentDTO.sportsInterests[1]=新字符串(“排球”); studentDTO.sportsInterests[2]=新字符串(“Carom”); GurdianInfo父亲=新的GurdianInfo(); father.gurdianName=“父亲姓名”; father.relation=“父亲”; 父地址=新地址(); father.address.adrLine1=“father addressline1”; father.address.adrLine2=“father addressline2”; father.address.city=“父城”; father.address.postalCode=11; father.gurdianCntactInfo=新ArrayList(); ContactInfo fContactInfo=新的ContactInfo(); fContactInfo.isPrimary=true; fContactInfo.cellNumber=“1234”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“5678”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“”; fContactInfo.emailId=“父亲电子邮件”; fContactInfo.type=“电子邮件”; 父.gurdianCntactInfo.add(fContactInfo); studentDTO.gurdianInfo=新建ArrayList(); studentDTO.gurdianInfo.add(父亲); GurdianInfo母亲=新的GurdianInfo(); mother.gurdianName=“父名”; 母亲关系=“父亲”; mother.address=新地址(); mother.address.adrLine1=“Father addressline1”; mother.address.adrLine2=“Father addressline2”; mother.address.city=“父城”; mother.address.postalCode=11; mother.gurdianCntactInfo=新的ArrayList(); ContactInfo mContactInfo=新的ContactInfo(); mContactInfo.isPrimary=true; mContactInfo.cellNumber=“PQRS”; mContactInfo.emailId=“”; mContactInfo.type=“单元格”; mother.gurdianCntactInfo.add(mContactInfo); mContactInfo=新联系人信息(); mContactInfo.isPrimary=false; mContactInfo.cellNumber=“”; mContactInfo.emailId=“母亲电子邮件”; mContactInfo.type=“电子邮件”; mother.gurdianCntactInfo.add(mContactInfo); studentDTO.gurdianInfo.add(母亲); 课程详细信息:课程详细信息=新课程详细信息(); 课程详情。课程名称=“MCA”; 课程详情.standard=“硕士”; CoursolamDetail.subjects=新字符串[3]; 课程详情。科目[0]=“SUB0”; 课程详情。科目[1]=“SUB1”; 课程详情。科目[2]=“SUB2”; studentDTO.curloamdetail=课程详情; //上面的部分只是创建了一个对象 //主代码从下面开始 ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); JSONObject studentJSON=新的JSONObject(); convertToJSON(studentDTO,studentJSON); System.out.println(studentJSON.toJSONString());//此行打印{} } 公共静态void convertToJSON(Object-Object,JSONObject-JSONObject)引发异常{ ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); 字符串jsonStr=mapperbj.writeValueAsString(对象); ; JSONParser=新的JSONParser(); JSONObject local=(JSONObject)parser.parse(jsonStr); jsonObject=本地; writeValueAsString(对象); System.out.println(“在CONEVRSION之后**************”+jsonObject.toJSONString());//此行打印JSON }_Java_Json_Object - Fatal编程技术网

jackson的对象到JSON转换问题 导入java.io.File; 导入java.lang.reflect.Array; 导入java.lang.reflect.Field; 导入java.math.BigDecimal; 导入java.sql.Time; 导入java.sql.Timestamp; 导入java.util.ArrayList; 导入java.util.Date; 导入java.util.List; 导入org.apache.commons.lang3.StringUtils; 导入org.json.simple.JSONArray; 导入org.json.simple.JSONObject; 导入org.json.simple.parser.JSONParser; 导入com.fasterxml.jackson.annotation.jsonautodect.Visibility; 导入com.fasterxml.jackson.annotation.PropertyAccessor; 导入com.fasterxml.jackson.databind.ObjectMapper; 导入com.fasterxml.jackson.databind.ObjectWriter; 导入com.report.mongo.MakeMD5Checksum; 导入com.report.mongo.MongoService; 导入job.com.rpt.bootstrap.ApplicationConfigCache; import service.com.rpt.db.MongoDataService; import service.com.rpt.util.RPTUtil; 公共类DTOtoJSONTest{ 公共静态void main(字符串[]args)引发异常{ //TODO自动生成的方法存根 Address studentAddress1=新地址(); studentaddress 1.adrLine1=“studentaddress 1 adrLine1”; studentaddress 1.adrLine2=“studentaddress 1 adrLine2”; studentaddress 1.city=“studentaddress 1城市”; studentAddress1.postalCode=1; Address studentAddress2=新地址(); studentaddress 2.adrLine1=“studentaddress 2 adrLine1”; studentaddress 2.adrLine2=“studentaddress 2 adrLine2”; studentaddress 2.city=“studentaddress 2城市”; 学生地址2.postalCode=2; StudentDTO StudentDTO=新StudentDTO(); studentDTO.name=“BK”; studentDTO.roll=1; 学生年龄=40; studentDTO.dateOfBirth=新日期(); studentDTO.gender=“M”; studentDTO.addresses=新地址[2]; studentDTO.addresses[0]=studentAddress1; 学生地址[1]=学生地址2; studentDTO.sportsInterests=新字符串[3]; studentDTO.sportsInterests[0]=新字符串(“Fooball”); studentDTO.sportsInterests[1]=新字符串(“排球”); studentDTO.sportsInterests[2]=新字符串(“Carom”); GurdianInfo父亲=新的GurdianInfo(); father.gurdianName=“父亲姓名”; father.relation=“父亲”; 父地址=新地址(); father.address.adrLine1=“father addressline1”; father.address.adrLine2=“father addressline2”; father.address.city=“父城”; father.address.postalCode=11; father.gurdianCntactInfo=新ArrayList(); ContactInfo fContactInfo=新的ContactInfo(); fContactInfo.isPrimary=true; fContactInfo.cellNumber=“1234”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“5678”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“”; fContactInfo.emailId=“父亲电子邮件”; fContactInfo.type=“电子邮件”; 父.gurdianCntactInfo.add(fContactInfo); studentDTO.gurdianInfo=新建ArrayList(); studentDTO.gurdianInfo.add(父亲); GurdianInfo母亲=新的GurdianInfo(); mother.gurdianName=“父名”; 母亲关系=“父亲”; mother.address=新地址(); mother.address.adrLine1=“Father addressline1”; mother.address.adrLine2=“Father addressline2”; mother.address.city=“父城”; mother.address.postalCode=11; mother.gurdianCntactInfo=新的ArrayList(); ContactInfo mContactInfo=新的ContactInfo(); mContactInfo.isPrimary=true; mContactInfo.cellNumber=“PQRS”; mContactInfo.emailId=“”; mContactInfo.type=“单元格”; mother.gurdianCntactInfo.add(mContactInfo); mContactInfo=新联系人信息(); mContactInfo.isPrimary=false; mContactInfo.cellNumber=“”; mContactInfo.emailId=“母亲电子邮件”; mContactInfo.type=“电子邮件”; mother.gurdianCntactInfo.add(mContactInfo); studentDTO.gurdianInfo.add(母亲); 课程详细信息:课程详细信息=新课程详细信息(); 课程详情。课程名称=“MCA”; 课程详情.standard=“硕士”; CoursolamDetail.subjects=新字符串[3]; 课程详情。科目[0]=“SUB0”; 课程详情。科目[1]=“SUB1”; 课程详情。科目[2]=“SUB2”; studentDTO.curloamdetail=课程详情; //上面的部分只是创建了一个对象 //主代码从下面开始 ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); JSONObject studentJSON=新的JSONObject(); convertToJSON(studentDTO,studentJSON); System.out.println(studentJSON.toJSONString());//此行打印{} } 公共静态void convertToJSON(Object-Object,JSONObject-JSONObject)引发异常{ ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); 字符串jsonStr=mapperbj.writeValueAsString(对象); ; JSONParser=新的JSONParser(); JSONObject local=(JSONObject)parser.parse(jsonStr); jsonObject=本地; writeValueAsString(对象); System.out.println(“在CONEVRSION之后**************”+jsonObject.toJSONString());//此行打印JSON }

jackson的对象到JSON转换问题 导入java.io.File; 导入java.lang.reflect.Array; 导入java.lang.reflect.Field; 导入java.math.BigDecimal; 导入java.sql.Time; 导入java.sql.Timestamp; 导入java.util.ArrayList; 导入java.util.Date; 导入java.util.List; 导入org.apache.commons.lang3.StringUtils; 导入org.json.simple.JSONArray; 导入org.json.simple.JSONObject; 导入org.json.simple.parser.JSONParser; 导入com.fasterxml.jackson.annotation.jsonautodect.Visibility; 导入com.fasterxml.jackson.annotation.PropertyAccessor; 导入com.fasterxml.jackson.databind.ObjectMapper; 导入com.fasterxml.jackson.databind.ObjectWriter; 导入com.report.mongo.MakeMD5Checksum; 导入com.report.mongo.MongoService; 导入job.com.rpt.bootstrap.ApplicationConfigCache; import service.com.rpt.db.MongoDataService; import service.com.rpt.util.RPTUtil; 公共类DTOtoJSONTest{ 公共静态void main(字符串[]args)引发异常{ //TODO自动生成的方法存根 Address studentAddress1=新地址(); studentaddress 1.adrLine1=“studentaddress 1 adrLine1”; studentaddress 1.adrLine2=“studentaddress 1 adrLine2”; studentaddress 1.city=“studentaddress 1城市”; studentAddress1.postalCode=1; Address studentAddress2=新地址(); studentaddress 2.adrLine1=“studentaddress 2 adrLine1”; studentaddress 2.adrLine2=“studentaddress 2 adrLine2”; studentaddress 2.city=“studentaddress 2城市”; 学生地址2.postalCode=2; StudentDTO StudentDTO=新StudentDTO(); studentDTO.name=“BK”; studentDTO.roll=1; 学生年龄=40; studentDTO.dateOfBirth=新日期(); studentDTO.gender=“M”; studentDTO.addresses=新地址[2]; studentDTO.addresses[0]=studentAddress1; 学生地址[1]=学生地址2; studentDTO.sportsInterests=新字符串[3]; studentDTO.sportsInterests[0]=新字符串(“Fooball”); studentDTO.sportsInterests[1]=新字符串(“排球”); studentDTO.sportsInterests[2]=新字符串(“Carom”); GurdianInfo父亲=新的GurdianInfo(); father.gurdianName=“父亲姓名”; father.relation=“父亲”; 父地址=新地址(); father.address.adrLine1=“father addressline1”; father.address.adrLine2=“father addressline2”; father.address.city=“父城”; father.address.postalCode=11; father.gurdianCntactInfo=新ArrayList(); ContactInfo fContactInfo=新的ContactInfo(); fContactInfo.isPrimary=true; fContactInfo.cellNumber=“1234”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“5678”; fContactInfo.emailId=“”; fContactInfo.type=“单元格”; 父.gurdianCntactInfo.add(fContactInfo); fContactInfo=新联系人信息(); fContactInfo.isPrimary=false; fContactInfo.cellNumber=“”; fContactInfo.emailId=“父亲电子邮件”; fContactInfo.type=“电子邮件”; 父.gurdianCntactInfo.add(fContactInfo); studentDTO.gurdianInfo=新建ArrayList(); studentDTO.gurdianInfo.add(父亲); GurdianInfo母亲=新的GurdianInfo(); mother.gurdianName=“父名”; 母亲关系=“父亲”; mother.address=新地址(); mother.address.adrLine1=“Father addressline1”; mother.address.adrLine2=“Father addressline2”; mother.address.city=“父城”; mother.address.postalCode=11; mother.gurdianCntactInfo=新的ArrayList(); ContactInfo mContactInfo=新的ContactInfo(); mContactInfo.isPrimary=true; mContactInfo.cellNumber=“PQRS”; mContactInfo.emailId=“”; mContactInfo.type=“单元格”; mother.gurdianCntactInfo.add(mContactInfo); mContactInfo=新联系人信息(); mContactInfo.isPrimary=false; mContactInfo.cellNumber=“”; mContactInfo.emailId=“母亲电子邮件”; mContactInfo.type=“电子邮件”; mother.gurdianCntactInfo.add(mContactInfo); studentDTO.gurdianInfo.add(母亲); 课程详细信息:课程详细信息=新课程详细信息(); 课程详情。课程名称=“MCA”; 课程详情.standard=“硕士”; CoursolamDetail.subjects=新字符串[3]; 课程详情。科目[0]=“SUB0”; 课程详情。科目[1]=“SUB1”; 课程详情。科目[2]=“SUB2”; studentDTO.curloamdetail=课程详情; //上面的部分只是创建了一个对象 //主代码从下面开始 ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); JSONObject studentJSON=新的JSONObject(); convertToJSON(studentDTO,studentJSON); System.out.println(studentJSON.toJSONString());//此行打印{} } 公共静态void convertToJSON(Object-Object,JSONObject-JSONObject)引发异常{ ObjectMapper mapperObj=新的ObjectMapper(); mapperObj.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); 字符串jsonStr=mapperbj.writeValueAsString(对象); ; JSONParser=新的JSONParser(); JSONObject local=(JSONObject)parser.parse(jsonStr); jsonObject=本地; writeValueAsString(对象); System.out.println(“在CONEVRSION之后**************”+jsonObject.toJSONString());//此行打印JSON },java,json,object,Java,Json,Object,} 在上面的代码中,我试图使用jackson将一个对象转换为JSON对象。对象正在转换为JSON对象,但问题是JSON对象仅在convertToJSON方法中可用(它打印JSON)。我无法在main方法中打印JSON。在main方法中,它打印“{}”。请帮忙 感谢您将convertToJSON中的jsonObject的引用更改为新对象studentJson保持相同的空对象。convertToJSON应返回JSONObject。不带走 import java.io.File; import jav

}

在上面的代码中,我试图使用jackson将一个对象转换为JSON对象。对象正在转换为JSON对象,但问题是JSON对象仅在convertToJSON方法中可用(它打印JSON)。我无法在main方法中打印JSON。在main方法中,它打印“{}”。请帮忙


感谢您将
convertToJSON
中的
jsonObject
的引用更改为新对象
studentJson
保持相同的空对象。convertToJSON应返回JSONObject。不带走
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 

}