Postgresql使用代码块处理json数据

最近得系统开发需要使用大量的json数据,但我们不需要整个json数据,只是需要其中的一部分。如果我们把大量的json数据一起返回交給前端去解析的话,会有不少问题(服务器网络带宽不够,加载缓慢;前端使用不方便异步解析,同步的话可能会卡)。

因此我需要将json数据在数据库或后台解析好再返回给前端,这次我选择使用数据去解析。


数据结构:(方法中的数据并不是这个,只是结构相同)

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              117.30809315600004,
              37.85365858600005
            ],
            [
              117.30810474500004,
              37.85365067300006
            ],
            [
              117.3083180220001,
              37.85376526500005
            ],
            [
              117.30855982600008,
              37.85389518200003
            ],
            [
              117.30880271300009,
              37.85401861300005
            ],
            [
              117.30898424100008,
              37.85414220200005
            ],
            [
              117.3090703680001,
              37.854195496000045
            ],
            [
              117.30911266700002,
              37.85413974000005
            ],
            [
              117.30918098000006,
              37.85396314900004
            ],
            [
              117.30925258800005,
              37.85365576100003
            ],
            [
              117.30930082200007,
              37.85326327100006
            ],
            [
              117.30938278600001,
              37.85267907000008
            ],
            [
              117.3094311110001,
              37.85223284500006
            ],
            [
              117.30960860000005,
              37.852239987000075
            ],
            [
              117.31003155500002,
              37.85228038300005
            ],
            [
              117.31060755900012,
              37.85233539500007
            ],
            [
              117.31066441900009,
              37.852340824000066
            ],
            [
              117.31067735600004,
              37.85217253800005
            ],
            [
              117.31068234600002,
              37.85207460700008
            ],
            [
              117.31068896300008,
              37.85194473400003
            ],
            [
              117.31068936700001,
              37.85178362800008
            ],
            [
              117.31088262300011,
              37.85180954900005
            ],
            [
              117.31155038600002,
              37.85191991800008
            ]
          ]
        ]
      },
      "properties": {
       
      }
    }
  ]
}

解析数据的方法

do
  $$
    declare
      i integer;
      a integer;
      b uuid;
    begin
      i := 0;
      b = '5c551fd0-acdf-49aa-a02d-84be31fdd86b';
      a = (SELECT json_array_length(A.JSON::json -> 'features') FROM "Json" A WHERE A.uid = b);
      for i in 0..a
        loop
          INSERT INTO "TB_COMMON_BZP" (name, latitude, longitude)
          VALUES ((SELECT
                       ((A.json::json -> 'features' -> i)::json -> 'properties')::json ->> 'BZ' place
                   FROM "Json" A
                   WHERE A.uid = b)::varchar, (SELECT
                                                   (((A.json::json -> 'features' -> i)::json -> 'geometry')::json ->
                                                    'coordinates')::json -> 0
                                               FROM "Json" A
                                               WHERE A.uid = b)::varchar, (SELECT
                                                                               (((A.json::json -> 'features' -> i)::json -> 'geometry')::json ->
                                                                                'coordinates')::json -> 1 lat
                                                                           FROM "Json" A
                                                                           WHERE A.uid = b)::varchar);
        end loop;
    end;
    $$

 

转载自:https://blog.csdn.net/qq_36213352/article/details/86699242

You may also like...

退出移动版