简体   繁体   English

使用GSON从Ajax请求解析JSON数组

[英]Using GSON to parse a JSON array from Ajax request

my json string is: 我的json字符串是:

[{"name":"51","value":"0"},{"name":"52","value":"0"},{"name":"53","value":"5"},{"name":"54","value":"1"},{"name":"106","value":"0"},{"name":"107","value":"0"},{"name":"108","value":"3"}]

I am trying to do: 我想做:

Gson gson = new Gson();
    class Wrapper{
        String name;
        String value;       
    }
Wrapper[] arr = gson.fromJson(request.getParameter("data"), Wrapper[].class);

And then: 接着:

for (Wrapper arr1 : arr) {
        if(Long.parseLong(arr1.value)!=0){
            listeProduits.add(ServiceMetier.getProduitById(Long.parseLong(arr1.name)));
            listeQuantites.add(Long.parseLong(arr1.value));
        }
    }

But I am always getting: 但我总是得到:

May 08, 2017 11:32:06 AM org.apache.catalina.core.StandardWrapperValve 
invoke
SEVERE: Servlet.service() for servlet [ActionServlet] in context with path 
[/Gustatif_Web] threw exception
java.lang.NullPointerException

The null pointer is on arr1. 空指针在arr1上。 What am I doing wrong with my json parsing? 我的json解析我做错了什么?

Edit: one problem was having the Wrapper class inside the method, another problem was deeper within the dependencies to another project. 编辑:一个问题是在方法中有Wrapper类,另一个问题是在另一个项目的依赖项中更深层次。

i have try it ; 我试过了; your code has a little error; 你的代码有一点错误; you must create a new java class-- Wrapper,do not be the inner class of this Method. 你必须创建一个新的java类--Wrapper,不要成为这个Method的内部类。 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM