将数组转换为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
最满意答案
我认为您只需要尝试从上面看到这些事情,以便确定流程中的角色并检测任务创建者,任务处理器和优先级控制器之间的依赖关系。 之后,为每个角色设计干净的可重用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' ...
-
生产者 - 消费者问题(Producer-Consumer Issue)[2022-02-12]
你在退出程序之前就错过了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 ...
-
python threading具有线程安全性的队列生产者 - 消费者(python threading Queue producer-consumer with thread-safe)[2023-01-20]
我建议你阅读生产者 - 消费者问题 。 你的生产者是获取线程。 您的消费者是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 ...
-
生产者 - 消费者队列,能够将项目移动到前面(Producer-consumer queue with the ability to move items to the front)[2022-08-14]
从我的评论: 您可以使用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 ...
-
这个生产者 - 消费者实施中是否存在竞争条件?(Are there race conditions in this producer-consumer implementation?)[2023-04-26]
有很多可能性 最明显的是:如果有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; ...