如何将查询转换为数组json

时间:2017-11-23 03:06:25

标签: javascript arrays json oracle jsp

我有一个查询,它给出了这样的输出:

public sealed class DroidCameraPreview : ViewGroup, ISurfaceHolderCallback
{
    public SurfaceView surfaceView;
    public ISurfaceHolder holder;
    Camera.Size previewSize;
    IList<Camera.Size> supportedPreviewSizes;
    IWindowManager windowManager;

    public bool IsPreviewing { get; set; }

    Camera _camera;
    public Camera Preview
    {
        get { return _camera; }
        set
        {
            _camera = value;
            if (_camera != null)
            {
                supportedPreviewSizes = Preview.GetParameters().SupportedPreviewSizes;
                RequestLayout();
            }
        }
    }

    private MediaRecorder _mediaRecorder;

    public MediaRecorder Recorder
    {
        get { return _mediaRecorder; }
        set { _mediaRecorder = value; }
    }

    public DroidCameraPreview(Context context)
        : base(context)
    {
        surfaceView = new SurfaceView(context);
        AddView(surfaceView);
        windowManager = Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        IsPreviewing = false;
        holder = surfaceView.Holder;
        holder.AddCallback(this);
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int width = ResolveSize(SuggestedMinimumWidth, widthMeasureSpec);
        int height = ResolveSize(SuggestedMinimumHeight, heightMeasureSpec);
        SetMeasuredDimension(width, height);
        if (supportedPreviewSizes != null)
        {
            previewSize = GetOptimalPreviewSize(supportedPreviewSizes, width, height);
        }
    }

    protected override void OnLayout(bool changed, int l, int t, int r, int b)
    {
        var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
        var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);
        surfaceView.Measure(msw, msh);
        surfaceView.Layout(0, 0, r - l, b - t);
    }

    public void SurfaceCreated(ISurfaceHolder holder)
    {
        try
        {
            if (Preview != null)
            {
                Preview.SetPreviewDisplay(holder);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(@"           ERROR: ", ex.Message);
        }
    }

    public void SurfaceDestroyed(ISurfaceHolder holder)
    {
        if (Preview != null)
        {
            Preview.StopPreview();
        }
    }

    public void SurfaceChanged(ISurfaceHolder holder, Android.Graphics.Format format, int width, int height)
    {
        var parameters = Preview.GetParameters();
        parameters.SetPreviewSize(previewSize.Width, previewSize.Height);
        RequestLayout();
        switch (windowManager.DefaultDisplay.Rotation)
        {
            case SurfaceOrientation.Rotation0:
                Preview.SetDisplayOrientation(90);
                break;
            case SurfaceOrientation.Rotation90:
                Preview.SetDisplayOrientation(0);
                break;
            case SurfaceOrientation.Rotation270:
                Preview.SetDisplayOrientation(180);
                break;
        }
        Preview.SetParameters(parameters);
        Preview.StartPreview();
        IsPreviewing = true;
    }

    Camera.Size GetOptimalPreviewSize(IList<Camera.Size> sizes, int w, int h)
    {
        const double AspectTolerance = 0.1;
        double targetRatio = (double)w / h;
        if (sizes == null)
        {
            return null;
        }
        Camera.Size optimalSize = null;
        double minDiff = double.MaxValue;
        int targetHeight = h;
        foreach (Camera.Size size in sizes)
        {
            double ratio = (double)size.Width / size.Height;

            if (Math.Abs(ratio - targetRatio) > AspectTolerance)
                continue;
            if (Math.Abs(size.Height - targetHeight) < minDiff)
            {
                optimalSize = size;
                minDiff = Math.Abs(size.Height - targetHeight);
            }
        }
        if (optimalSize == null)
        {
            minDiff = double.MaxValue;
            foreach (Camera.Size size in sizes)
            {
                if (Math.Abs(size.Height - targetHeight) < minDiff)
                {
                    optimalSize = size;
                    minDiff = Math.Abs(size.Height - targetHeight);
                }
            }
        }
        return optimalSize;
    }        
}

这是我的疑问:

public class CameraPreviewRenderer : ViewRenderer<MyApp.CameraPreview, MyApp.Droid.DroidCameraPreview>
{
    DroidCameraPreview cameraPreview;

    protected override void OnElementChanged(ElementChangedEventArgs<MyApp.CameraPreview> e)
    {
        base.OnElementChanged(e);
        if (Control == null)
        {
            cameraPreview = new DroidCameraPreview(Context);
            SetNativeControl(cameraPreview);
        }
        if (e.OldElement != null)
        {
            e.OldElement.TakePictureChanged -= TakePicture;
            e.OldElement.StartVideoRecordChanged -= StartVideoRecord;
            e.OldElement.StopVideoRecordChanged -= StopVideoRecord;
        }
        if (e.NewElement != null)
        {
            Control.Preview = Camera.Open((int)e.NewElement.CameraOptions);
            Control.Recorder = new MediaRecorder();
            e.NewElement.TakePictureChanged += TakePicture;
            e.NewElement.StartVideoRecordChanged += StartVideoRecord;
            e.NewElement.StopVideoRecordChanged += StopVideoRecord;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            Control.Preview.Release();
            Control.Recorder.Release();
        }
        base.Dispose(disposing);
    }

    private async void TakePicture(object sender, EventArgs e)
    {
        PictureCallback pc = new PictureCallback(cameraPreview.MediaFilename.FullName + "jpg");
        Control.Preview.TakePicture(pc, pc, pc);
        await pc.TCS.Task;
    }

    private class PictureCallback : Java.Lang.Object, Camera.IPictureCallback, Camera.IShutterCallback
    {
        private string FileName;
        public TaskCompletionSource<bool> TCS = new TaskCompletionSource<bool>();
        public PictureCallback(string fileName)
        {
            FileName = fileName;
        }

        public void OnPictureTaken(byte[] data, Camera camera)
        {
            File pictureFile = new File(FileName);
            FileOutputStream fos = null;
            if (data != null)
            {
                try
                {
                    fos = new FileOutputStream(pictureFile);
                    fos.Write(data);
                    fos.Flush();
                    TCS.SetResult(true);
                }
                catch (FileNotFoundException e)
                {
                    TCS.SetException(e);
                }
                catch (IOException ie)
                {
                    TCS.SetException(ie);
                }
                finally
                {
                    fos?.Close();
                }
            }
        }
        public void OnShutter() { }
    }

    private void StartVideoRecord(object sender, EventArgs e)
    {
        //Control.Preview.StopPreview();
        Control.Preview.Unlock();
        Control.Recorder.SetVideoSource(VideoSource.Camera);
        Control.Recorder.SetAudioSource(AudioSource.Mic);
        Control.Recorder.SetOutputFormat(OutputFormat.Default);
        Control.Recorder.SetVideoEncoder(VideoEncoder.Default);
        Control.Recorder.SetAudioEncoder(AudioEncoder.Default);
        Control.Recorder.SetOutputFile(cameraPreview.MediaFilename.FullName + "mp4");
        Control.Recorder.SetPreviewDisplay(Control.holder.Surface);
        Control.Recorder.Prepare();
        Control.Recorder.Start();
    }

    private void StopVideoRecord(object sender, EventArgs e)
    {
        Control.Recorder.Stop();
        Control.Recorder.Reset();
        //Control.Recorder.Release();
        Control.Preview.Lock();
        //Control.Preview.Reconnect();
        //Control.Preview.SetPreviewDisplay(Control.holder);
        Control.Preview.StartPreview();
    }
}

在javascript中我插入一个数组变量,如下所示:

{ key:1,label:"R. Bulan"}
{ key:2,label:"R. Bintang"}
{ key:3,label:"R. Akasia"}
{ key:4,label:"R. Guest Room"}

,输出变为:

select '{ '||'key:'||IDMEETINGROOM||''||',label:"'||NAMARUANG||'"'||'} ' json
    from MMEETING_ROOM  where IDLOKASI='I' order by IDMEETINGROOM;

如何使输出不是字符串数组,但应如下所示:

var event = new array();
 event.push(<query>);

3 个答案:

答案 0 :(得分:0)

你必须从字符串解析回Object,使用JSON.parse()  转换它。

更新11:50

好的,问题是:你的返回数据不是JSON有效,它缺少双引号,所以JSON.parse无法为你解析它

所以我在这里用字符串替换做了一些技巧:

    data = data.getElementsByTagName('SEARCH')[0].childNodes;
    var sections=new Array();
    if (data.length > 0){
        for(var i=0;i<data.length;i++){
            var item = JSON.parse(Url.decode(data[i].chil‌​dNodes[0].firstChild‌​.data).replace(/([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\""))
            sections.push(item);
        }
    }
    console.log(sections);

答案 1 :(得分:0)

很少有观察结果:

  • 您的output没有有效的JSON

    enter image description here

  • 使用JSON.Parse()方法解析JSON string,然后将结果JSON Object推送到event数组。

    var event = new array();
    event.push(JSON.parse(<query>));
    

然后结果输出将如下

console.log(event);

输出

[{
        "key": 1,
        "label": "R.Bulan"
    }, {
        "key": 2,
        "label": "R.Bintang"
    },
    {
        "key": 3,
        "label": "R.Akasia"
    },
    {
        "key": 4,
        "label": "R.Guest Room"
    }
]

答案 2 :(得分:0)

Oracle已经建立了对JSON的支持(自12.1.0.2起)。 您可以使用JSON_OBJECT和JSON_ARRAYAGG来创建所需的JSON。 见下文:

create table MMEETING_ROOM (IDMEETINGROOM NUMBER,  
                            NAMARUANG VARCHAR2(40));
insert into MMEETING_ROOM values(1,'R. Bulan');
insert into MMEETING_ROOM values(2,'R. Bintang');
insert into MMEETING_ROOM values(3,'R. Akasia');
insert into MMEETING_ROOM values(4,'R. Guest Room');


commit;

SELECT JSON_OBJECT('key' VALUE IDMEETINGROOM,
                   'label' VALUE NAMARUANG)
FROM MMEETING_ROOM;


JSON_OBJECT('KEY'VALUEIDMEETINGROOM,'LABEL'VALUENAMARUANG)
---------------------------------------------------------------
{"key":1,"label":"R. Bulan"}
{"key":2,"label":"R. Bintang"}
{"key":3,"label":"R. Akasia"}
{"key":4,"label":"R. Guest Room"}

并创建数组

SELECT JSON_ARRAYAGG( JSON_OBJECT('key' VALUE IDMEETINGROOM,
                                  'label' VALUE NAMARUANG))
FROM MMEETING_ROOM;

JSON_ARRAYAGG(JSON_OBJECT('KEY'VALUEIDMEETINGROOM,'LABEL'VALUENAMARUANG))
-------------------------------------------------------------------------
[{"key":1,"label":"R. Bulan"},
 {"key":2,"label":"R. Bintang"},
 {"key":3,"label":"R. Akasia"},
 {"key":4,"label":"R. Guest Room"}]