如何创建JSON数组 [英] How to Create JSON Array

查看:175
本文介绍了如何创建JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jsp或java创建类似的内容:

How to create something like this using jsp or java :

{
    "Maths" : [ 
        {
            "Name"  : "Amit",        // First element
            "Marks" : 67,
            "age"   : 23
        }, {
            "Name"  : "Sandeep",    // Second element
            "Marks" : 65,
            "age"   : 21
        }
    ], "Science" : [
        {
            "Name"  : "Shaili",     // First Element
            "Marks" : 56,
            "age"   : 27
        }, {
            "Name"  : "Santosh",    // Second Element
            "Marks" : 78,
            "age"   : 41
        }
   ]
}

推荐答案

首先,将所有代码缩进4个空格,否则它将变得难以辨认.其次,您可能无法生成有意义的评论(为什么还要这么做?).

First, indent all code by 4 spaces, otherwise it becomes illegible. Second, you probably can't generate meaningful comments (and why would you even want to?).

对于代码,了解要输出的起始结构将很有帮助.没有这个,我可以告诉您两种方法:

As for the code, it would be helpful to know the starting structure that you want to output. Without that, I can tell you of two approaches:

  1. 迭代您的结构,然后递归打印内容

  1. Iterate on your structures, and print contents recursively

意识到它已经完成了,请在此处中查找.的确,格森也很好.基本上,使用Google"java gson",您得到的答案将比摇摇晃晃的多.

Realise it's been already done, and look here. Indeed, Gson is also good. Basically, google "java gson" and you'll have more answers than you can shake a stick at.

这篇关于如何创建JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆