2016-09-28 103 views
0

這是JSON代碼我的Java代碼中添加Java數據,如何使用JSON解析

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代碼。 謝謝

+0

請說明一下:您試圖解決什麼問題?你期望輸出什麼?你試過什麼了? – BPS

+0

首先,我已經做了這個java類,現在我想在這個對象中添加值,比如使用json數據的地址和位置。我應該使用哪些代碼? @BPS – devjava20

回答

1
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"]} 
0

有幾個框架爲你做JSON解析。

FasterXML可能是使用最廣泛的。

http://wiki.fasterxml.com/JacksonHome

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

谷歌的番石榴還提供了此功能。