如何在json和java中创建子项,以及如何在javascript中获取子项的值?

时间:2014-03-08 08:30:52

标签: java javascript json jsp

使用此代码,我可以生成

下面的结构
 int n=3;
        String json []= new String [n];       
        try {
            JSONArray js = new JSONArray();
            ArrayList<String> ciudades;
            ciudades = new ArrayList<String>();
            ciudades.add("tokio");
            ciudades.add("madrid");
            ciudades.add("santiago");
                JSONObject j;
            for (int x = 0; x < ciudades.size(); x++) {
                ArrayList<Integer> temp;              
               temp = new ArrayList<Integer>();
               for(int z=0;z<6;z++){
                  int temperatura = x+z;
                   temp.add(temperatura);
               }
                j = new JSONObject();
              j.put("name", ciudades.get(x));
               j.put("data", temp);
             js.put(j);
          }  
          json[0] = js.toString();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally {
            String valor="json1";
           request.setAttribute(valor, json[0]); 
           RequestDispatcher dispatcher = context.getRequestDispatcher("/datos.jsp");
           dispatcher.forward(request, response);

我创建的结构是

[
{"name":"tokio","data":[0,1,2,3,4,5]},
{"name":"madrid","data":[1,2,3,4,5,6]},
{"name":"santiago","data":[2,3,4,5,6,7]}
]

我需要创建的结构是

"paises": {
    "pais": [
      {"name":"tokio","data":[0,1,2,3,4,5]},
      {"name":"madrid","data":[1,2,3,4,5,6]},
      {"name":"santiago","data":[2,3,4,5,6,7]}
    ]
  }

我在javascript中收到变量

var np= ${json1};
var datos = np;

如何根据需要生成列表以及如何在javascript中读取二级结构?

1 个答案:

答案 0 :(得分:1)

使用Gson(以及lombok的@Data支持类)和first version of your question

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;

public class Json {
    public static void main(String[] args)
    {
        final Wrapper wrapper = new Wrapper();
        final Paises paises = wrapper.getPaises();
        paises.getPais().add(Pais.of("Costa Rica", "San José"));
        paises.getPais().add(Pais.of("México", "DF"));
        paises.getPais().add(Pais.of("Argentina", "Buenos Aires"));

        final Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(wrapper));
    }

    @Data
    private static class Wrapper {
        private Paises paises = new Paises();
    }

    @Data
    private static class Paises {
        private List<Pais> pais = new ArrayList<Pais>();
    }

    @Data(staticConstructor = "of")
    private static class Pais {
        private final String nombre;
        private final String capital;
    }
}

<强>输出:

{
  "paises": {
    "pais": [
      {
        "nombre": "Costa Rica",
        "capital": "San José"
      },
      {
        "nombre": "México",
        "capital": "DF"
      },
      {
        "nombre": "Argentina",
        "capital": "Buenos Aires"
      }
    ]
  }
}