我如何更改字符串字段的值(格式json)?

时间:2019-02-11 15:37:09

标签: java json

我正在用Java开发Web服务。

在服务中,我必须检查“ desagg”字段的值(如果它等于0),则不必执行任何操作;如果它大于0,则必须进行另一个查询并将结果放在适当的位置json中的“ desArticle”字段的值

我尝试使用Google图书馆的gson方法,但我认为这不是正确的方法

String sql="SELECT sigdoc as sigla, numdoc as numero, datdoc as data , procor AS seq_articolo, desagg , precorpo.codint as codiceInterno, qtauni AS quantitaArticolo, " + 
                " przuni AS prezzoVendita, (scont0/100) AS sconto1, (scont1/100) AS sconto2, (scont2/100) AS sconto3, impnet AS importoNetto, datcon as dataConsegna, serdoc as chiaveRiga," + 
                " coarfo as codiceArticolo, descri as desArticolo" + 
                " FROM precorpo " + 
                " INNER JOIN anamagge ON precorpo.codint=anamagge.codint " + 
                " WHERE tiprig=0 AND sigdoc='"+sigdoc+"' AND numdoc="+numdoc+"" + 
                " ORDER BY procor;";

try
        {

            json = db.executeQueryTOJSON2(sql);

            JsonObject jobj = new Gson().fromJson(json, JsonObject.class);

            int result = jobj.get("desagg").getAsInt();

            if (result > 0)
            {
                String query2=" select group_concat(descri) from memodocu where pointm="+result+" group by pointm;";

                ResultSet rs = db.executeQuery(query2);
                while(rs.next())
                {
                    String desc = rs.getString("group_concat(descri)");
                }
            }


        }
        catch(Exception e)
        {
            System.out.println("errore "+e);
        }
输出中的

json为:

[ { "sigla":"PREV" , "numero":1.1000122E7 , "data":"" , "seq_articolo":1 , "desagg":0 , "codiceInterno":25951 , "quantitaArticolo":2.0 , "prezzoVendita":4.62 , "sconto1":0.0000 , "sconto2":0.0000 , "sconto3":0.0000 , "importoNetto":9.24 , "dataConsegna":"" , "chiaveRiga":7379 , "codiceArticolo":"A025951" , "desArticolo":"025951 ARTICOLO DI PROVA" }  , { "sigla":"PREV" , "numero":1.1000122E7 , "data":"" , "seq_articolo":2 , "desagg":100 , "codiceInterno":15879 , "quantitaArticolo":20.0 , "prezzoVendita":2.17 , "sconto1":0.0000 , "sconto2":0.0000 , "sconto3":0.0000 , "importoNetto":43.4 , "dataConsegna":"" , "chiaveRiga":7380 , "codiceArticolo":"A015879" , "desArticolo":"I want to modify this" }  ] 

我是json的初学者

3 个答案:

答案 0 :(得分:0)

Gson是正确的方法,根据文档,您可以解析字符串,然后通过给定的键来查找类型值

此处是文档:

https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonElement.html#getAsInt--

JsonObject jobj = new Gson().fromJson(mySqlResultAsJson, JsonObject.class);



int result = jobj.get("desagg").getAsInt();

if (result >= 0)
{
    do another request
}

答案 1 :(得分:0)

如果要解析此内容:

{ "sigla":"PREV" , "numero":1.1000122E7 , "data":"" , "seq_articolo":2 , "desagg":100 , "codiceInterno":15879 , "quantitaArticolo":20.0 , "prezzoVendita":2.17 , "sconto1":0.0000 , "sconto2":0.0000 , "sconto3":0.0000 , "importoNetto":43.4 , "dataConsegna":"" , "chiaveRiga":7380 , "codiceArticolo":"A015879" , "desArticolo":"I want to modify this" }

您可能会做类似的事情;

MyObject myObj = new Gson().fromJson(myJsonStream, MyObject.class);
int desagg = myObj.getDesagg();

并定义一个这样的对象:

class MyObject {
  private String sigla; 
  private float numero; 
  private String data; 
  ...
}

答案 2 :(得分:0)

我解决了问题!

import myTsModule from 'my-ts-module.ts'

我使用jsonArray解析json字符串和json对象以获取所需字段的值,然后如果tmp与0不同,我将运行查询以获取结果并更新desArticle字段的值

最终结果将最终存储在jsonarray变量上,要进行打印,您必须执行jsonarray.toString();