首页 \ 问答 \ 将数组转换为JSON(Converting an array into JSON)

将数组转换为JSON(Converting an array into JSON)

我需要引入一个csv文档并将其转换为JSON,到目前为止,我已经能够将它转换为数组,并从数组中我试图构建一个JSON对象。

下面是构建JSON的JavaScript,但它不在我需要的结构中,下面是所需结构的示例。

var jsonObj = []; //declare object

for (var i=1;i<csvAsArray.length;i++) { 
  jsonObj.push({key: csvAsArray[i][0]}); //key

    for (var l=1;l<csvAsArray[0].length;l++) { 
      jsonObj.push({label: csvAsArray[0][l], values: csvAsArray[i][l]}); //label + value respectively
    }
}

最终输出要求:

{
  "key": "Sample 01",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 1
    } , 
    { 
      "label" : "Something" ,
      "value" : 2
    }
  ]
},
{
  "key": "Sample 02",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 5
    } , 
    { 
      "label" : "Something" ,
      "value" : 4
    }
  ]
}

I need to bring in a csv doc and convert it to JSON, so far I have been able to convert it to an array and from the array I'm trying to build a JSON object.

Below is the JavaScript that builds the JSON, but its not in the structure I need, underneath is an example of the structure required.

var jsonObj = []; //declare object

for (var i=1;i<csvAsArray.length;i++) { 
  jsonObj.push({key: csvAsArray[i][0]}); //key

    for (var l=1;l<csvAsArray[0].length;l++) { 
      jsonObj.push({label: csvAsArray[0][l], values: csvAsArray[i][l]}); //label + value respectively
    }
}

Final output required:

{
  "key": "Sample 01",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 1
    } , 
    { 
      "label" : "Something" ,
      "value" : 2
    }
  ]
},
{
  "key": "Sample 02",
  "values": [
    { 
      "label" : "Something" ,
      "value" : 5
    } , 
    { 
      "label" : "Something" ,
      "value" : 4
    }
  ]
}

原文:https://stackoverflow.com/questions/19262738
更新时间:2021-10-15 14:10

最满意答案

我认为您只需要尝试从上面看到这些事情,以便确定流程中的角色并检测任务创建者,任务处理器和优先级控制器之间的依赖关系。 之后,为每个角色设计干净的可重用EJBs / Services。

有时,对于只需要10-20秒的相对较短的任务,强制用户使用异步EJB(搜索@Asynchronous )而不是创建任务更有意义。

关于OptimisticLockExpcetions:这些可能发生是因为数据同时发生更改,这可能是由任务使用者的其他线程之一或客户端导致的,这会更改数据。 找出原因,如果这是第一种情况,请纠正错误。 当然,如果你提供一些代码或解释如何运作,你会得到更多的帮助。

处理任务时:我会用悲观锁取出任务实体,以便另一个线程不会开始处理相同的任务。

所以我想你的过程很复杂,你需要一个更好/更灵活的设计。


I think you just need to try to see the things from above in order to identify the roles in the process & detect the dependencies between the creator of the tasks, the processor(s) of the task and the priority-controller. After that, design for every of these roles clean reusable EJBs/Services.

Sometimes, for relative short tasks that take just 10-20 seconds it makes more sense to force the user to wait, using Asynchronous EJBs (search for @Asynchronous), instead of creating a task.

About the OptimisticLockExpcetions: these can occur because the data changes in the meanwhile, which can be caused by one of your other threads of the task-consumer, or by the client, which changes the data. Identify the cause and if that is the first case, correct the bug. Of course you would get more help you if you provide some code or explanations on how everything works.

When processing the tasks: I would fetch the Task entities with Pessimistic Lock, so that another thread does not begin to process the same task.

So I suppose your process is complex and your need a better/more flexible design.

相关问答

更多
  • 在java.util中没有一种标准的方法可以做到这一点,但这是一个简单的设计: 我假设A,B,C都实现接口D,并且如果A与B和C无序处理则无关紧要。 拥有一组“输入”BlockingQueue和一组排序消费者。 这些使用者确定队列中对象的类型,并将它们提供给真实工作者所从的独立的特定于类型的“输出”队列。 是否存在一些要求您只有一个队列的约束? There isn't a standard way to do this in java.util, but here's a simple design: I' ...
  • 你在退出程序之前就错过了pthread_join: .... pthread_join(producer,NULL); 在pthread_join(消费者,NULL); 返回0; .... you're missing pthread_join before exiting the program: .... pthread_join(producer,NULL); pthread_join(consumer,NULL); return 0; ....
  • 我认为您只需要尝试从上面看到这些事情,以便确定流程中的角色并检测任务创建者,任务处理器和优先级控制器之间的依赖关系。 之后,为每个角色设计干净的可重用EJBs / Services。 有时,对于只需要10-20秒的相对较短的任务,强制用户使用异步EJB(搜索@Asynchronous )而不是创建任务更有意义。 关于OptimisticLockExpcetions:这些可能发生是因为数据同时发生更改,这可能是由任务使用者的其他线程之一或客户端导致的,这会更改数据。 找出原因,如果这是第一种情况,请纠正错误。 ...
  • 如果您使用的是有界队列,则还可以考虑使用ArrayBlockingQueue 。 另外,来自LinkedBlockingQueue Javadoc: 链接队列通常具有比基于阵列的队列更高的吞吐量,但在大多数并发应用程序中具有较低的可预测性能。 If you are using a bounded queue, you could also consider an ArrayBlockingQueue. Also, from the LinkedBlockingQueue Javadoc: Linked qu ...
  • 我建议你阅读生产者 - 消费者问题 。 你的生产者是获取线程。 您的消费者是save功能。 如果我理解正确,您希望消费者尽快保存获取的结果。 为此,生产者和消费者必须能够以某种线程安全的方式(例如队列)进行通信。 基本上,您需要另一个队列。 它将取代proxy_array 。 您的save功能将如下所示: while True: try: data = fetch_data_from_output_queue() save_to_database(data) except EmptyQueu ...
  • 是的,你可以很好地完成Concurrent ML中的消息传递。 不要被系统时代推迟; John Reppy的书和论文是这个话题的绝佳指南。 美丽的东西! Yes, you can do it quite nicely with message passing in Concurrent ML. Don't be put off by the age of the system; John Reppy's book and papers are excellent guides to the topic. B ...
  • 从我的评论: 您可以使用remove(Object o)来检索对象并将其从队列中删除,然后使用addFirst(Object o)将其添加到队列的前面。 这将具有优先处理此对象的效果。 您可以使用迭代器来检查Deque中的对象。 但是要非常小心ConcurrentModificationException因为如果您在一个线程中访问Collection并在另一个线程中修改它,则可能会引发它们,但是如果您使用的是blockingQueue,则这不应该是一个问题。 来自BlockingQueue Javadoc: ...
  • coda->head == MAX_QUEUE-1 这不会检查队列是否已满。 有两个变量描述队列的状态, head和tail 。 coda->head == coda->tail 这会正确检查队列是否为空。 请注意检查中如何使用这两个变量。 coda->head == MAX_QUEUE-1 This does not check whether the queue is full. There are two variables that describe the state of the queu ...
  • 有很多可能性 最明显的是:如果有2个生产者生产数据。 假设缓冲区中只有1个可用空间,两个生产者线程都可以通过while (in + 1) % BUFFER SIZE) == out并尝试放入缓冲区。 这可能导致缓冲区损坏或数据丢失 即使只有1个消费者和1个生产者,仍然存在一些不太明显的问题。 例如,编译器可能会重新排列行 buffer[in] = next_produced; in = (in + 1) % BUFFER SIZE; 使更新发生在buffer更新之前,这会导致消费者访问未初始化的数据。 T ...
  • 您的消费者在第一次运行中结束。 当它第一次执行时,缓冲区应为空,然后它不会进入while循环并结束run方法和Thread。 你必须循环等待缓冲区,有时,如果缓冲区为空,这不应该结束消费者。 它只是等待下一条消息。 您可以使用以下代码执行此操作: public class TwitterStreamConsumer implements Runnable{ private final BlockingQueue> tweetBuffer; ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)