简体   繁体   English

在json数组中找到双引号

[英]double quotes found in json array

a json array is as given below json数组如下所示

var data = [
            {label:'gggg',data: [[(new Date('2011/12/01')).getTime(),53914],[(new Date('2012/1/02')).getTime(),32172],[(new Date('2012/2/03')).getTime(),824],[(new Date('2012/4/04')).getTime(),838],[(new Date('2012/6/05')).getTime(),755],[(new Date('2012/7/06')).getTime(),0],[(new Date('2012/8/07')).getTime(),0],[(new Date('2012/9/08')).getTime(),0],[(new Date('2012/10/09')).getTime(),0],[(new Date('2012/11/10')).getTime(),0],[(new Date('2012/12/11')).getTime(),0],[(new Date('2012/12/11')).getTime(),0]]}

        ];

in java class for creating the above similar json, i'm using the following code given below. 在用于创建上述类似json的java类中,我使用下面给出的以下代码。
but the problem is there is a double quotes in each "(new Date(2012/12/01)).getTime()" 但问题是每个 "(new Date(2012/12/01)).getTime()" 中都有双引号

can anyone please tell me how to remove those double quotes 谁能告诉我如何删除那些双引号

 Query q1=session.createQuery("FROM VendorMonth");
          List li1=q1.list();


          String supname="",tempsupname;  
          JSONObject obj = new JSONObject();
          JSONArray jsonarrmast = new JSONArray();
          List s=new ArrayList();

          JSONArray finals=new JSONArray();
          JSONArray finalarray = new JSONArray();
          for(int i=0;i<li1.size();i++)
          {
            HashMap hmap = new HashMap();
            VendorMonth venmonth=(VendorMonth) li1.get(i);
            tempsupname=venmonth.getId().getSupplierName();
            if(i==0){
                supname=venmonth.getId().getSupplierName(); 
            }

            if(!supname.equals(tempsupname)){
                obj.put("label", supname);
                obj.put("data", jsonarrmast); 
                jsonarrmast = new JSONArray();
                s.add(obj);
                finalarray.put(obj);
                obj = new JSONObject();
                supname=venmonth.getId().getSupplierName();

                JSONArray jsonarr = new JSONArray();
                String date=venmonth.getId().getYearnam()+"/"+venmonth.getId().getMonthnam()+"/01";
                String ss=new String("(new Date("+date+")).getTime()");
                jsonarr.put(ss);
                jsonarr.put(venmonth.getId().getRentalrate());
                jsonarrmast.put(jsonarr);
            }
            else
            {
                JSONArray jsonarr = new JSONArray();
                String date=venmonth.getId().getYearnam()+"/"+venmonth.getId().getMonthnam()+"/01";
                String ss=new String("(new Date("+date+")).getTime()");
                jsonarr.put(ss);
                jsonarr.put(venmonth.getId().getRentalrate());
                jsonarrmast.put(jsonarr);

            }

            if(i==(li1.size()-1)){
               obj.put("label", supname);
               obj.put("data", jsonarrmast); 
               jsonarrmast = new JSONArray();
               s.add(obj); 
               finalarray.put(obj);
            }


          }

but i'm getting the output as given below 但是我得到的输出如下

[{"data":[["(new Date(2012/12/01)).getTime()",10976.23],["(new Date(2013/1/01)).getTime()",51213.8200000002],["(new Date(2013/2/01)).getTime()",32172.31],["(new Date(2013/3/01)).getTime()",824.600000000001],["(new Date(2013/4/01)).getTime()",838.000000000001],["(new Date(2013/5/01)).getTime()",755.780000000001],["(new Date(2013/6/01)).getTime()",50877.12]],"label":"Weather Ford"},{"data":[["(new Date(2012/12/01)).getTime()",24368.3],["(new Date(2013/1/01)).getTime()",1968.76]],"label":"Logan Tools"},{"data":[["(new Date(2012/12/01)).getTime()",3425.63],["(new Date(2013/1/01)).getTime()",731.75]],"label":"Pioneer tools"}]

You're not going to be able to create a JSON object that matches your declaration, because that's not a JSON object: it's Javascript code. 您将无法创建与您的声明相匹配的JSON对象,因为它不是JSON对象:它是Javascript代码。

Once that Javascript code is ran, however, data will contain an object that can be serialized to JSON, and I'm assuming that's what you're trying to achieve. 但是,一旦运行了Javascript代码, data将包含一个可以序列化为JSON的对象,而我假设这就是您要实现的目标。

What your Java code does is add a String to a BasicDBArray - the fact that it's interpreted as a String should not come as a surprise. 您的Java代码所做的就是将String添加到BasicDBArray -将其解释为String这一事实不足为奇。 By the same token, when you add an int or a boolean, they're added as ints and booleans, not strings. 同样,当您添加int或boolean时,它们将被添加为int和boolean,而不是字符串。

What you actuall want to put in your BasicDBArray is the value that new Date('2011/12/01').getTime() would return if interpreted as Javascript: the number of milliseconds between 1970/01/01 and 2011/12/01. 实际想在BasicDBArray中输入的值是如果将new Date('2011/12/01').getTime()解释为Javascript将返回的值:1970/01/01至2011/12 /之间的毫秒数01。 I'm assuming you can retrieve that through something like venmonth.getId().getDate().getTime() , or however it is you retrieve a Date instance from your venmonth object. 我假设您可以通过venmonth.getId().getDate().getTime()之类的方法来检索该内容,或者它是您从venmonth对象中检索一个Date实例。

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

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