有人可以提供java代码来创建一个json对象,如下所示
{"main":[
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"]
]}
我尝试了类似
的内容 Gson gson = new Gson();
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("One"));
array.add(new JsonPrimitive("two"));
array.add(new JsonPrimitive("three"));
array.add(new JsonPrimitive("four"));
array.add(new JsonPrimitive("five"));
JsonObject jsonObject = new JsonObject();
jsonObject.add("main", array);
即使我在循环
,我也会得到如下结果{"main":["one","two","three","four","five"]}
就像一个单一的对象。但我期待结果如
{"main":[
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"],
["one","two","three","four","five"]
]}
非常感谢提前。
答案 0 :(得分:1)
尝试使用此代码创建json
Gson gson = new Gson();
JsonArray array = new JsonArray();
JsonArray child = new JsonArray();
child.add(new JsonPrimitive("One"));
child.add(new JsonPrimitive("two"));
child.add(new JsonPrimitive("three"));
child.add(new JsonPrimitive("four"));
child.add(new JsonPrimitive("five"));
for(int i=0;i<5;i++)
array.add(child);
JsonObject jsonObject = new JsonObject();
jsonObject.add("main", array);
System.out.println(jsonObject);
答案 1 :(得分:1)
假设您正在使用Gson,这不是它的设计使用方式。虽然支持这种方式,但不建议使用,因为您可以使用任何json库来执行此操作(SimpleJson)。
相反,Gson能够直接序列化我们熟悉的java对象,因此您应该将json对象表示为java对象。 JsonObject映射到Map。 JsonArray映射到List或数组。 JsonPrimitives映射到它们各自的java原始类型(boolean,double,string,null)
// generate the object
Map<List<List<String>>> object = new HashMap<>();
List<List<String>> main = new ArrayList<>();
List<String> counts = Arrays.asList("one", "two", "three", "four", "five");
for (int i = 0; i < 5; i++) {
main.add(counts);
}
object.put("main", main);
// serialize it
String json = new Gson().toJson(object);
// deserializing it requires a typetoken or separate class representing the map object.
Map<List<List<String>>> desObj = new Gson().fromJson(json, new TypeToken<Map<List<List<String>>>>(){}.getType());
答案 2 :(得分:0)
看来"main"
包含一个数组数组,所以您只需要将array
五次添加到另一个新数组(例如将其称为mainArray
)然后添加mainArray
的{{1}}:
jsonObject
mainArray
五次添加到array
mainArray
添加到mainArray
答案 3 :(得分:0)
您可以尝试将表示json数据的字符串转换为JsonObject:
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class JsonQuestion {
public static void main(String[] args) {
String myJSON = "{\"main\":[\n"
+ " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
+ " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
+ " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
+ " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n"
+ " [\"one\",\"two\",\"three\",\"four\",\"five\"]\n"
+ "]}";
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(myJSON);
System.out.println("jsonObject: " + jsonObject.toString());
}
}
答案 4 :(得分:0)
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
<script>
var modal = document.getElementById('myModal');
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
<?php
$sql = "select * from enquires2";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<tr>
<td>
<span><img src='gridview/view.png' alt='View' id='myBtn<?php echo $row['id']; ?>'></span>
</td>
</tr>
<div id='myModal' class='modal'>
<div class='modal-content'>
<span class='close'>×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<?php
}
?>
答案 5 :(得分:0)
您可以使用以下方法制作json:
private void createJsonData(){
final String[] units = {"One","Two","Three","Four",
"Five"};
try {
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONArray jsonArray1 = new JSONArray();
for (int j = 0; j < 5 ; j++) {
jsonArray1.put(units[j])
}
jsonArray.put(jsonArray);
jsonObject.put("main",jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 6 :(得分:0)
使用此方法
public static void newjson() {
JSONObject json =new JSONObject();
List<List<String>> listoflist = new ArrayList<List<String>>();
List<String> list=new ArrayList<String>();
list.add("one");
list.add("one");
list.add("one");
listoflist.add(list);
listoflist.add(list);
try {
json.put("main",listoflist);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(json);
}