如何使用Java构造json对象

时间:2018-10-22 11:57:39

标签: java json

{  
    "lastRanDate":"2018-08-12",
    "statusCode":"200",
    "errorCode":null,
    "statusCodeShortDesc":null,
    "statusMessage":null,
    "programs":[  
        {  
            "name":"program1",
            "programData":{  
                "SNCount":"5000",
                "progAggregate":{  
                    "percentage":"90%",
                    "severity":"medium"
                },
                "sites":[  
                    {  
                        "name":"FXZZ",
                        "siteData":{  
                            "siteAggregate":{  
                                "percentage":"90%",
                                "severity":"medium"
                            },
                            "dates":[  
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                },
                                {  
                                    "date":"24 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                }
                            ]
                        }
                    },
                    {  
                        "name":"FXZZ",
                        "siteData":{  
                            "siteAggregate":{  
                                "percentage":"90%",
                                "severity":"medium"
                            },
                            "dates":[  
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                },
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        },
        {  
            "name":"program2",
            "programData":{  
                "SNCount":"5000",
                "progAggregate":{  
                    "percentage":"90%",
                    "severity":"medium"
                },
                "sites":[  
                    {  
                        "name":"FXZZ",
                        "siteData":{  
                            "siteAggregate":{  
                                "percentage":"90%",
                                "severity":"medium"
                            },
                            "dates":[  
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                },
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                }
                            ]
                        }
                    },
                    {  
                        "name":"FXZZ",
                        "siteData":{  
                            "siteAggregate":{  
                                "percentage":"90%",
                                "severity":"medium"
                            },
                            "dates":[  
                                {  
                                    "date":"23 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                },
                                {  
                                    "date":"24 May",
                                    "dateAggregate":"90%",
                                    "dateData":[  
                                        {  
                                            "name":"table1",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table2",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table3",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table4",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table5",
                                            "value":"90%"
                                        },
                                        {  
                                            "name":"table6",
                                            "value":"90%"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

我得到的是json响应,我必须用Java构造此响应并传递给其他人api,我是json的新手,请帮助我

2 个答案:

答案 0 :(得分:1)

看看Jackson,这是一个很好的教程供您遵循:https://www.baeldung.com/jackson-object-mapper-tutorial

答案 1 :(得分:0)

Java没有用于解析和存储JSON对象的任何内置方法。您可以使用Google的第三方库GSON或JSONParser来以Java处理JSON。

  1. 从Google下载并配置SimpleJson
  2. 导入下载的软件包。
  3. 然后使用以下代码来解析JSON:

    JSONParser解析器=新的JSONParser();

    JSONObject json =(JSONObject)parser.parse(stringToParse);

json是您的JSON对象。