Django结合两个json对象形成一个新数组 [英] Django combine two json objects to form a new array

查看:46
本文介绍了Django结合两个json对象形成一个新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个json对象

I have two json objects as follow

Id:1

 {"points":[{"x":109,"y":286,"r":1,"color":"black"},{"x":108,"y":285,"r":1,"color":"black"},{"x":106,"y":282,"r":1,"color":"black"},{"x":103,"y":276,"r":1,"color":"black"},],"lines":[{"x1":109,"y1":286,"x2":108,"y2":285,"strokeWidth":"2","strokeColor":"black"},{"x1":108,"y1":285,"x2":106,"y2":282,"strokeWidth":"2","strokeColor":"black"},{"x1":106,"y1":282,"x2":103,"y2":276,"strokeWidth":"2","strokeColor":"black"}]}

Id-2

{"points":[{"x":524,"y":343,"r":1,"color":"black"},{"x":523,"y":342,"r":1,"color":"black"},{"x":521,"y":339,"r":1,"color":"black"},{"x":520,"y":334,"r":1,"color":"black"},{"x":514,"y":319,"r":1,"color":"black"}],"lines":[{"x1":524,"y1":343,"x2":523,"y2":342,"strokeWidth":"2","strokeColor":"black"},{"x1":523,"y1":342,"x2":521,"y2":339,"strokeWidth":"2","strokeColor":"black"},{"x1":521,"y1":339,"x2":520,"y2":334,"strokeWidth":"2","strokeColor":"black"},{"x1":520,"y1":334,"x2":514,"y2":319,"strokeWidth":"2","strokeColor":"black"}]}

我正在尝试将这两个数据合并到画布上我能够检索单个文件,但是将它们合并却无法执行

I am trying to merge these two data onto a canvas I am able to retrieve a single file but combining them i am not able to do

def loadDrawing(request):
    """ Function to load the drawing with drawingID if it exists."""
    try:
        # Getting JSON object string of saved drawing.
        drawingJSONData = Drawing.objects.get(id = 1).drawingJSONText

        # drawingJSONData1 = Drawing.objects.get(id=1).drawingJSONText
       # drawingJSONData2 = Drawing.objects.get(id=2).drawingJSONText

        # Seding context with appropriate information
        context = {
            "loadIntoJavascript" : True,
            "JSONData" : drawingJSONData

        }
        # Editing response headers and returning the same
        response = modifiedResponseHeaders(render(request, 'MainCanvas/index.html', context))
        return response

我在Django中的模型

My model in django

class Drawing(models.Model):
    drawingJSONText = models.TextField(null = True)

我的.js文件,用于加载图形并将其从服务器解析为JSON对象,并将加载的点推入数组中

My .js file to load the drawing and parsing it from server to JSON object and pushing the loaded points into an array

 // Checking if the drawing to be loaded exists
    if (document.getElementById('JSONLoadData') != null)
    {

        // Parsing the loaded drawing from server to a JSON Object
        var loadedData = JSON.parse(JSONLoadData.value)

        // Iterating through all the points in the loaded drawing
        for(let i = 0; i < loadedData['points'].length; i++)
        {
            // Saving the point and drawing the same in the svg canvas
            const point = svg.append('circle')
                             .attr('cx', loadedData['points'][i]['x'])
                             .attr('cy', loadedData['points'][i]['y'])
                             .attr('r', loadedData['points'][i]['r'])
                             .style('fill', loadedData['points'][i]['color']);

            // Pushing the point inside points array
            points.push(point);
        }

        // Iterating through all the lines in the loaded drawing
        for(let i = 0; i < loadedData['lines'].length; i++)
        {
            // Saving the line and drawing the same in the svg canvas
            const line = svg.append('line')
                            .attr('x1', loadedData['lines'][i]['x1'])
                            .attr('y1', loadedData['lines'][i]['y1'])
                            .attr('x2', loadedData['lines'][i]['x2'])
                            .attr('y2', loadedData['lines'][i]['y2'])
                            .attr('stroke-width', loadedData['lines'][i]['strokeWidth'])
                            .style('stroke', loadedData['lines'][i]['strokeColor']);

            // Pushing the line inside lines array
            lines.push(line);
        }

    }

});

如果我的模型如下

class Drawing(models.Model):
    drawingJSONText = models.TextField(null=True)
    project = models.CharField(max_length=250)

如何根据项目过滤数据可以说我有三个数据集

How can i filter data based on project Lets say i have three datasets

1st one contains project = a
2nd one contains project = b
3rd one contains project = a
4th one contains project = a

如何通过过滤数据来添加上述类似的数据点 Drawing.objects.filter(project = a)然后基于查询集,我有三个数据点,并且在画布上绘制了相应的数据,如上

How can i add datapoints like above by filtering data Drawing.objects.filter(project=a) then based on the queryset i have three data points and corresponding data are plotted on canvas as above

推荐答案

我不完全确定这是您想要的,但是您是否要结合使用id-1和id-2?如果我认为我了解您要做什么,那么在这种情况下,使用+运算符对您有用吗?

I'm not entirely sure this is what you want, but are you trying to combine id-1 and id-2? If I think I understand what you are trying to do, will using the + operator work for you in this case?

drawingJSONData1 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData2 = json.loads(Drawing.objects.get(id=1).drawingJSONText)

drawingJSONData = dict()
drawingJSONData["points"] = drawingJSONData1["points"]+drawingJSONData1["points"]
drawingJSONData["lines"] = drawingJSONData2["lines"]+drawingJSONData2["lines"]

在上面的示例中,您最终得到:

With your example above, you'd end up with:

{'points': [{'x': 109, 'y': 286, 'r': 1, 'color': 'black'},
  {'x': 108, 'y': 285, 'r': 1, 'color': 'black'},
  {'x': 106, 'y': 282, 'r': 1, 'color': 'black'},
  {'x': 103, 'y': 276, 'r': 1, 'color': 'black'},
  {'x': 524, 'y': 343, 'r': 1, 'color': 'black'},
  {'x': 523, 'y': 342, 'r': 1, 'color': 'black'},
  {'x': 521, 'y': 339, 'r': 1, 'color': 'black'},
  {'x': 520, 'y': 334, 'r': 1, 'color': 'black'},
  {'x': 514, 'y': 319, 'r': 1, 'color': 'black'}],
 'lines': [{'x1': 109,
   'y1': 286,
   'x2': 108,
   'y2': 285,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 108,
   'y1': 285,
   'x2': 106,
   'y2': 282,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 106,
   'y1': 282,
   'x2': 103,
   'y2': 276,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 524,
   'y1': 343,
   'x2': 523,
   'y2': 342,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 523,
   'y1': 342,
   'x2': 521,
   'y2': 339,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 521,
   'y1': 339,
   'x2': 520,
   'y2': 334,
   'strokeWidth': '2',
   'strokeColor': 'black'},
  {'x1': 520,
   'y1': 334,
   'x2': 514,
   'y2': 319,
   'strokeWidth': '2',
   'strokeColor': 'black'}]}

添加了包裹在 json.loads 中的获取信息,可将字符串转换为Python对象,因为我不知道是哪种类型,并且鉴于出现了错误.

added the gets wrapped in json.loads to convert string to Python object as I don't know what kind of field that is and given the error being seen.

这篇关于Django结合两个json对象形成一个新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆