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

我正在用java开发一个Web服务。


在服务中,我必须检查“desagg”字段的值,如果它等于 0,我不必做任何事情,而如果它大于 0,我必须进行另一个查询并将结果代替值json 中的“desArticle”字段


我尝试使用谷歌图书馆的 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的初学者


隔江千里
浏览 171回答 3
3回答

哆啦的时光机

如果你想解析这个:{ "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;   ...}

千万里不及你

我解决了这个问题!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonarray = new JSONArray(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonarray.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonobject = jsonarray.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String tmp = jsonobject.getString("desagg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!tmp.equals("0"))//vuol dire che dessagg diverso da 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql2="select group_concat(descri) from memodocu where pointm="+tmp+" group by pointm;";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet rs = db.executeQuery(sql2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String tmp2="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(rs.next())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp2=rs.getString("group_concat(descri)");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonobject.put("desArticolo", tmp2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }我使用 jsonArray 来解析 json 字符串和 json 对象来获取我想要的字段的值然后如果 tmp 不同于 0 我去运行查询我获取结果并更新字段 desArticle 的值最终结果将在 jsonarray 变量上结束,要打印它你必须做 jsonarray.toString ();

慕森王

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){&nbsp; &nbsp; do another request}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java