我有一个json文件,如:
{
"a1" : {
"Contestant 1 marks" : 44,
"Contestant 10 marks" : 30,
"Contestant 11 marks" : 15,
"Contestant 12 marks" : 35,
"Contestant 13 marks" : 28,
"Contestant 14 marks" : 8,
"Contestant 2 marks" : 35,
"Contestant 3 marks" : 30.899999618530273,
"Contestant 4 marks" : 39,
"Contestant 5 marks" : 45,
"Contestant 6 marks" : 47,
"Contestant 7 marks" : 17,
"Contestant 8 marks" : 37,
"Contestant 9 marks" : 35
},
"a2" : {
"Contestant 1 marks" : 35,
"Contestant 10 marks" : 30,
"Contestant 11 marks" : 15,
"Contestant 12 marks" : 35,
"Contestant 13 marks" : 28,
"Contestant 14 marks" : 8,
"Contestant 2 marks" : 30,
"Contestant 3 marks" : 30.899999618530273,
"Contestant 4 marks" : 39,
"Contestant 5 marks" : 45,
"Contestant 6 marks" : 47,
"Contestant 7 marks" : 17,
"Contestant 8 marks" : 37,
"Contestant 9 marks" : 35
}
}
这里似乎有两个JSON对象a1,a2。这些对象本身有14个JSON对象。
我的String completeJson;
包含完整的JSON。
现在我写的时候
JSONObject jsonRootObj = new JSONObject(completeJson);
然后whilch json对象将进入jsonRootObj?
另外,我如何获得密钥的值:"参赛者1标记"对于我的JSON文件中的a1和a2?
我想在a1
中添加对象的值,其中a2
中的对象值具有相同的KEY。
答案 0 :(得分:0)
您可以将解析a1
和a2
作为JSONObject,然后更改值。
JSONObject jsonRootObj = JSON.parseObject(completeJson);
JSONObject a1 = jsonRootObj.getJSONObject("a1");
JSONObject a2 = jsonRootObj.getJSONObject("a2");
for (Map.Entry<String, Object> entry : a1.entrySet()) {
if (a2.containsKey(entry.getKey())) {
a2.put(entry.getKey(), entry.getValue());
}
}
P.S:我的JSON解析器是fastjson
答案 1 :(得分:0)
以这种方式从A1获得值:
long valA1 = ((Long)((JSONObject)jo.get("a1")).get("Contestant 1 marks")).longValue();
以这种方式你输入A2:
((JSONObject)((JSONObject)jo.get("a2")).get("Contestant 1 marks")).put("Contestant 1 marks", valA1);
其中jo是:
JSONParser jp = new JSONParser();
JSONObject jo = (JSONObject)jp.parse(<your json string>);
答案 2 :(得分:0)
让我们让您的生活更轻松。
下载gson library。
右键单击项目,构建路径 - >添加外部存档。
然后用你想要的任何名称创建一个类(我使用了Example.java):
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("a1")
@Expose
private A1 a1;
@SerializedName("a2")
@Expose
private A2 a2;
/**
* @return The a1
*/
public A1 getA1() {
return a1;
}
/**
* @param a1 The a1
*/
public void setA1(A1 a1) {
this.a1 = a1;
}
/**
* @return The a2
*/
public A2 getA2() {
return a2;
}
/**
* @param a2 The a2
*/
public void setA2(A2 a2) {
this.a2 = a2;
}
public class A1 {
@SerializedName("Contestant 1 marks")
@Expose
private int contestant1Marks;
@SerializedName("Contestant 10 marks")
@Expose
private int contestant10Marks;
@SerializedName("Contestant 11 marks")
@Expose
private int contestant11Marks;
@SerializedName("Contestant 12 marks")
@Expose
private int contestant12Marks;
@SerializedName("Contestant 13 marks")
@Expose
private int contestant13Marks;
@SerializedName("Contestant 14 marks")
@Expose
private int contestant14Marks;
@SerializedName("Contestant 2 marks")
@Expose
private int contestant2Marks;
@SerializedName("Contestant 3 marks")
@Expose
private float contestant3Marks;
@SerializedName("Contestant 4 marks")
@Expose
private int contestant4Marks;
@SerializedName("Contestant 5 marks")
@Expose
private int contestant5Marks;
@SerializedName("Contestant 6 marks")
@Expose
private int contestant6Marks;
@SerializedName("Contestant 7 marks")
@Expose
private int contestant7Marks;
@SerializedName("Contestant 8 marks")
@Expose
private int contestant8Marks;
@SerializedName("Contestant 9 marks")
@Expose
private int contestant9Marks;
/**
* @return The contestant1Marks
*/
public int getContestant1Marks() {
return contestant1Marks;
}
/**
* @param contestant1Marks The Contestant 1 marks
*/
public void setContestant1Marks(int contestant1Marks) {
this.contestant1Marks = contestant1Marks;
}
/**
* @return The contestant10Marks
*/
public int getContestant10Marks() {
return contestant10Marks;
}
/**
* @param contestant10Marks The Contestant 10 marks
*/
public void setContestant10Marks(int contestant10Marks) {
this.contestant10Marks = contestant10Marks;
}
/**
* @return The contestant11Marks
*/
public int getContestant11Marks() {
return contestant11Marks;
}
/**
* @param contestant11Marks The Contestant 11 marks
*/
public void setContestant11Marks(int contestant11Marks) {
this.contestant11Marks = contestant11Marks;
}
/**
* @return The contestant12Marks
*/
public int getContestant12Marks() {
return contestant12Marks;
}
/**
* @param contestant12Marks The Contestant 12 marks
*/
public void setContestant12Marks(int contestant12Marks) {
this.contestant12Marks = contestant12Marks;
}
/**
* @return The contestant13Marks
*/
public int getContestant13Marks() {
return contestant13Marks;
}
/**
* @param contestant13Marks The Contestant 13 marks
*/
public void setContestant13Marks(int contestant13Marks) {
this.contestant13Marks = contestant13Marks;
}
/**
* @return The contestant14Marks
*/
public int getContestant14Marks() {
return contestant14Marks;
}
/**
* @param contestant14Marks The Contestant 14 marks
*/
public void setContestant14Marks(int contestant14Marks) {
this.contestant14Marks = contestant14Marks;
}
/**
* @return The contestant2Marks
*/
public int getContestant2Marks() {
return contestant2Marks;
}
/**
* @param contestant2Marks The Contestant 2 marks
*/
public void setContestant2Marks(int contestant2Marks) {
this.contestant2Marks = contestant2Marks;
}
/**
* @return The contestant3Marks
*/
public float getContestant3Marks() {
return contestant3Marks;
}
/**
* @param contestant3Marks The Contestant 3 marks
*/
public void setContestant3Marks(float contestant3Marks) {
this.contestant3Marks = contestant3Marks;
}
/**
* @return The contestant4Marks
*/
public int getContestant4Marks() {
return contestant4Marks;
}
/**
* @param contestant4Marks The Contestant 4 marks
*/
public void setContestant4Marks(int contestant4Marks) {
this.contestant4Marks = contestant4Marks;
}
/**
* @return The contestant5Marks
*/
public int getContestant5Marks() {
return contestant5Marks;
}
/**
* @param contestant5Marks The Contestant 5 marks
*/
public void setContestant5Marks(int contestant5Marks) {
this.contestant5Marks = contestant5Marks;
}
/**
* @return The contestant6Marks
*/
public int getContestant6Marks() {
return contestant6Marks;
}
/**
* @param contestant6Marks The Contestant 6 marks
*/
public void setContestant6Marks(int contestant6Marks) {
this.contestant6Marks = contestant6Marks;
}
/**
* @return The contestant7Marks
*/
public int getContestant7Marks() {
return contestant7Marks;
}
/**
* @param contestant7Marks The Contestant 7 marks
*/
public void setContestant7Marks(int contestant7Marks) {
this.contestant7Marks = contestant7Marks;
}
/**
* @return The contestant8Marks
*/
public int getContestant8Marks() {
return contestant8Marks;
}
/**
* @param contestant8Marks The Contestant 8 marks
*/
public void setContestant8Marks(int contestant8Marks) {
this.contestant8Marks = contestant8Marks;
}
/**
* @return The contestant9Marks
*/
public int getContestant9Marks() {
return contestant9Marks;
}
/**
* @param contestant9Marks The Contestant 9 marks
*/
public void setContestant9Marks(int contestant9Marks) {
this.contestant9Marks = contestant9Marks;
}
}
public class A2 {
@SerializedName("Contestant 1 marks")
@Expose
private int contestant1Marks;
@SerializedName("Contestant 10 marks")
@Expose
private int contestant10Marks;
@SerializedName("Contestant 11 marks")
@Expose
private int contestant11Marks;
@SerializedName("Contestant 12 marks")
@Expose
private int contestant12Marks;
@SerializedName("Contestant 13 marks")
@Expose
private int contestant13Marks;
@SerializedName("Contestant 14 marks")
@Expose
private int contestant14Marks;
@SerializedName("Contestant 2 marks")
@Expose
private int contestant2Marks;
@SerializedName("Contestant 3 marks")
@Expose
private float contestant3Marks;
@SerializedName("Contestant 4 marks")
@Expose
private int contestant4Marks;
@SerializedName("Contestant 5 marks")
@Expose
private int contestant5Marks;
@SerializedName("Contestant 6 marks")
@Expose
private int contestant6Marks;
@SerializedName("Contestant 7 marks")
@Expose
private int contestant7Marks;
@SerializedName("Contestant 8 marks")
@Expose
private int contestant8Marks;
@SerializedName("Contestant 9 marks")
@Expose
private int contestant9Marks;
/**
* @return The contestant1Marks
*/
public int getContestant1Marks() {
return contestant1Marks;
}
/**
* @param contestant1Marks The Contestant 1 marks
*/
public void setContestant1Marks(int contestant1Marks) {
this.contestant1Marks = contestant1Marks;
}
/**
* @return The contestant10Marks
*/
public int getContestant10Marks() {
return contestant10Marks;
}
/**
* @param contestant10Marks The Contestant 10 marks
*/
public void setContestant10Marks(int contestant10Marks) {
this.contestant10Marks = contestant10Marks;
}
/**
* @return The contestant11Marks
*/
public int getContestant11Marks() {
return contestant11Marks;
}
/**
* @param contestant11Marks The Contestant 11 marks
*/
public void setContestant11Marks(int contestant11Marks) {
this.contestant11Marks = contestant11Marks;
}
/**
* @return The contestant12Marks
*/
public int getContestant12Marks() {
return contestant12Marks;
}
/**
* @param contestant12Marks The Contestant 12 marks
*/
public void setContestant12Marks(int contestant12Marks) {
this.contestant12Marks = contestant12Marks;
}
/**
* @return The contestant13Marks
*/
public int getContestant13Marks() {
return contestant13Marks;
}
/**
* @param contestant13Marks The Contestant 13 marks
*/
public void setContestant13Marks(int contestant13Marks) {
this.contestant13Marks = contestant13Marks;
}
/**
* @return The contestant14Marks
*/
public int getContestant14Marks() {
return contestant14Marks;
}
/**
* @param contestant14Marks The Contestant 14 marks
*/
public void setContestant14Marks(int contestant14Marks) {
this.contestant14Marks = contestant14Marks;
}
/**
* @return The contestant2Marks
*/
public int getContestant2Marks() {
return contestant2Marks;
}
/**
* @param contestant2Marks The Contestant 2 marks
*/
public void setContestant2Marks(int contestant2Marks) {
this.contestant2Marks = contestant2Marks;
}
/**
* @return The contestant3Marks
*/
public float getContestant3Marks() {
return contestant3Marks;
}
/**
* @param contestant3Marks The Contestant 3 marks
*/
public void setContestant3Marks(float contestant3Marks) {
this.contestant3Marks = contestant3Marks;
}
/**
* @return The contestant4Marks
*/
public int getContestant4Marks() {
return contestant4Marks;
}
/**
* @param contestant4Marks The Contestant 4 marks
*/
public void setContestant4Marks(int contestant4Marks) {
this.contestant4Marks = contestant4Marks;
}
/**
* @return The contestant5Marks
*/
public int getContestant5Marks() {
return contestant5Marks;
}
/**
* @param contestant5Marks The Contestant 5 marks
*/
public void setContestant5Marks(int contestant5Marks) {
this.contestant5Marks = contestant5Marks;
}
/**
* @return The contestant6Marks
*/
public int getContestant6Marks() {
return contestant6Marks;
}
/**
* @param contestant6Marks The Contestant 6 marks
*/
public void setContestant6Marks(int contestant6Marks) {
this.contestant6Marks = contestant6Marks;
}
/**
* @return The contestant7Marks
*/
public int getContestant7Marks() {
return contestant7Marks;
}
/**
* @param contestant7Marks The Contestant 7 marks
*/
public void setContestant7Marks(int contestant7Marks) {
this.contestant7Marks = contestant7Marks;
}
/**
* @return The contestant8Marks
*/
public int getContestant8Marks() {
return contestant8Marks;
}
/**
* @param contestant8Marks The Contestant 8 marks
*/
public void setContestant8Marks(int contestant8Marks) {
this.contestant8Marks = contestant8Marks;
}
/**
* @return The contestant9Marks
*/
public int getContestant9Marks() {
return contestant9Marks;
}
/**
* @param contestant9Marks The Contestant 9 marks
*/
public void setContestant9Marks(int contestant9Marks) {
this.contestant9Marks = contestant9Marks;
}
}
}
之后,
String yourString = "json in String format";
Example example = new Gson().fromJson(yourString, Example.class);
example.getA1().getContestant1Marks();
example.getA2().getContestant1Marks();
多数民众赞成。您可以使用这些getter方法。
PS:我使用jsonschema2pojo创建了这个类,将json复制到它中。
快乐编码...... !!!
答案 3 :(得分:0)
您的JSON字符串包含无名根元素,从第一个{
开始到最后一个}
结束。该对象将进入jsonRootObj
变量。要获取a1
或a2
对象,请执行以下操作:
JSONObject a1 = jsonRootObj.getJSONObject("a1");
要获得a1
中第一位参赛者的价值,您可以这样做:
int marks = a1.getInt("Contestant 1 marks");
您可以找到JSONObject
here的完整Javadoc。没有必要拥有JSON数组。