繁体   English   中英

如何使用JSON解析在Java中添加数据

[英]how to add data in java using json parsing

这是我的json代码的Java代码,

package favr.com.example2;


public class Final {
    public Result1 result;
    public Infor info;

    Final(Result1 result,Infor info)
    {
        this.result=result;
        this.info=info;
    }

     class Result1 {
         String gender;
         UserName user;
         Location1 location1;

         public Result1(String gender, UserName user, Location1 location1) {
             this.gender = gender;
             this.user = user;
             this.location1 = location1;
         }

         private class UserName {
             String title;
              String first;
             String last;

             public UserName(String title, String first, String last) {
                 this.title = title;
                 this.first = first;
                 this.last = last;
             }
         }

         private class Location1 {
             String street;
             String city;
             String state;

             public Location1(String street, String city, String state) {
                 this.street = street;
                 this.city = city;
                 this.state = state;
             }
         }
     }

     class Infor {
         String seed;
         int results;
         int page;

         public Infor(String seed, int results, int page) {
             this.seed = seed;
             this.results = results;
             this.page = page;
         }
     }
}

json代码:

{

        results:[
        gender:"female",
        user:[
    title:"qwe",firstname:"dev" ,lastname:"java"
    ],
        location:[
    street:"abc",city:"def",state:"xyz"
    ]
        ]

        info:[
        seed:"abcdef",page:"1234",result:"1"
        ]
        }

现在我想使用json解析为名称性别和其他字段添加值,我该如何解决这个问题。 我试图搜索stackoverflow链接,但它们显示了有关使用Java访问json代码的信息。 谢谢

That code working fine:

 public class Final {


 public Result1 result;
 public Infor info;
 String userJosn      = "";
 String ResultJosn    = "";
 String locationJosn  = "";
 String infoJosn      = "";
 String finalJson     = "";

 Final(Result1 result,Infor info) {
     this.result=result;
     this.info=info;
 }

 public Final() {
 }

 class Result1 {

     String gender;
     UserName user;
     Location1 location1;

     public Result1(String gender, UserName user, Location1 location1) {
         this.gender = gender;
         this.user = user;
         this.location1 = location1;
         ResultJosn=ResultJosn+"results:[gender:\""+this.gender+"\","+userJosn+","+locationJosn+"]";
    }

     public Result1() {
     }

     private class UserName {
         String title;
         String first;
         String last;
         public UserName(String title, String first, String last) {
             this.title = title;
             this.first = first;
             this.last = last;
             userJosn=userJosn+"user:[title:\""+this.title +"\",firstname:\""+this.first+"\",lastname:\""+this.last+"\"]";
         }
     }

     private class Location1 {
         String street;
         String city;
         String state;

         public Location1(String street, String city, String state) {
             this.street = street;
             this.city = city;
             this.state = state;
             locationJosn=locationJosn+"location:[street:\""+this.street +"\",city:\""+this.city+"\" ,state:\""+this.state+"\"]";
         }
     }
     String results="[gender:\""+gender+"\"]";
 }

 class Infor {
     String seed;
     int results;
     int page;

     public Infor(String seed, int results, int page) {
         this.seed = seed;
         this.results = results;
         this.page = page;
         infoJosn=infoJosn+"infoJosn:[seed:\""+this.seed +"\",results:\""+this.results+"\",page:\""+this.page+"\"]";
     }
 }

 public static void main(String[] args) {

     Final mFinal=new Final();
     Final.Result1 mResult=mFinal.new Result1();

     new Final( mFinal.new Result1("male",mResult.new UserName("Tamilan", "C.", "Peiyasamy"), 
            mResult.new Location1("south", "m.puram", "Tamilnadu")),mFinal.new Infor("test",1234,1));

     mFinal.finalJson="{"+mFinal.ResultJosn+""+mFinal.infoJosn+"}";
     System.out.println("\n "+mFinal.finalJson);
 }


}


output:

{results:[gender:"male",user:[title:"Tamilan",firstname:"C.",lastname:"Peiyasamy"],location:[street:"south",city:"m.puram" ,state:"Tamilnadu"]]infoJosn:[seed:"test",results:"1234",page:"1"]}

有几种框架可以为您执行JSON解析。

FasterXML可能是使用最广泛的。

http://wiki.fasterxml.com/JacksonHome

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.3</version>
</dependency>

Google的Guava也提供了此功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM