gpt4 book ai didi

java - 如何在java中使用json解析添加数据

转载 作者:行者123 更新时间:2023-12-01 09:27:02 26 4
gpt4 key购买 nike

这是我的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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com