作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的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"]}
关于java - 如何在java中使用json解析添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39752618/
我是一名优秀的程序员,十分优秀!