问:
List<Tasks> dont do anything when i run code but if i step through it works
When i execute the code, inspecting after WhenAll the idList is empty, if i step through it using breakpoints after the WhenAll everything works as expected.

This code is within a non-async method.

Its as if letting it run on its own is not starting the tasks.

```<code>var currentPage = 1;
List<Result> idList = new List<Result>();

var response = this.GetDocuments(currentPage).Result;
//idList.AddRange(response.results);

var tasks = new List<Task>();

for (var i = 0; i < response.meta.page.total_pages; ++i)
{
    var t = new Task(() =>
    {
        response = this.GetDocuments(i+1).Result;
        idList.AddRange(response.results);
    });
    tasks.Add(t);
    t.Start();
}

 Task.WhenAll(tasks).Wait();

 public async Task<ResponseObject> GetDocuments(int currentPage)
        {
            var result = await httpClient.GetAsync($"api/as/v1/engines/urlname/documents/list?page[current]={currentPage}").ConfigureAwait(false);

            if (!result.IsSuccessStatusCode)
            {
                loggly.Error($"Exception: ScheduledJobBase, IndexPages, failed when getting documents from Swiftype, response: { result }");
                return null;
            }

            return await result.Content.ReadAsAsync<ResponseObject>().ConfigureAwait(false); ;
        }
</code>```

答: The issue happened might be due to the collection is not synchronized, Might have thrown InvalidAccessException.</p>

Body:
When i execute the code, inspecting after WhenAll the idList is empty, if i step through it using breakpoints after the WhenAll everything works as expected.

This code is within a non-async method.

Its as if letting it run on its own is not starting the tasks.

var currentPage = 1;
List<Result> idList = new List<Result>();

var response = this.GetDocuments(currentPage).Result;
//idList.AddRange(response.results);

var tasks = new List<Task>();

for (var i = 0; i < response.meta.page.total_pages; ++i)
{
    var t = new Task(() =>
    {
        response = this.GetDocuments(i+1).Result;
        idList.AddRange(response.results);
    });
    tasks.Add(t);
    t.Start();
}

 Task.WhenAll(tasks).Wait();

 public async Task<ResponseObject> GetDocuments(int currentPage)
        {
            var result = await httpClient.GetAsync($"api/as/v1/engines/urlname/documents/list?page[current]={currentPage}").ConfigureAwait(false);

            if (!result.IsSuccessStatusCode)
            {
                loggly.Error($"Exception: ScheduledJobBase, IndexPages, failed when getting documents from Swiftype, response: { result }");
                return null;
            }

            return await result.Content.ReadAsAsync<ResponseObject>().ConfigureAwait(false); ;
        }


来源: stackexchange

问:
Overlay original numbers on histogram with proportions
I have a dataset with the following properties:

multiple locations

open for different lengths of time

with different carers present at different time points

Here's a bit of sample code:

```<code>dt <- tibble(
  "location" = c("A", "B", "C", "D", "A", "B", "C", "A", "B", "C", "A", "B", "A", "A", "A"),
  "months.since.start" = c(0,0,0,0,1,1,1,2,2,2,3,3,4,5,6),
  "carer" = c("HCA", "nurse", "nurse", "dr", "HCA", "dr", "nurse", "HCA", "dr", "nurse", "dr", "HCA", "dr", "nurse", "HCA")
)
</code>```

What I would like to do is ideally 2 different things:

overlay the histogram with the counts (plot A below) in line form on
top of the histogram with the proportions (plot B below)

overlay the number with the count of active locations (the geom_text in plot A) above the proportions (in plot B)

The plots I refer to:

I'm aware that they function on different axes which is addressed in this post, but I don't need the axis of plot A to be shown, its only to show the change in active locations over time.

I suspect the first part could be done by dividing the total count by 4 (i.e the total number of active locations) and then layering the histogram on top of the proportional count, but I'm unsure how to do that.

答:
I've managed to answer my own question - I thought I'd put it up rather than delete it, as I've not found this anywhere else (although I appreciate this is fairly simple code).

Here's the plot:

And here's the code for the above plot:

```<code>dt %>%
  ggplot(., size = 2, aes(months.since.start)) +
  geom_histogram(binwidth = 1,                  # original chart with proportions
                 position = "fill",
                 aes(fill = carer)) +
  geom_histogram(binwidth = 1,                  # the barchart with the total count
                 color = 'grey',
                 alpha = 0,                     # transparent boxes
                 aes(y=..count../4)) +          # divided by the total number of locations (4 in this case), so that it becomes a fraction of 1 and therefore will fit within the y-axis
  geom_text(stat = 'count',
            aes(label=..count..),
            position=position_fill(vjust=0.05), #the text, adjusted using position_fill, so that the position is fixed
            color = 'white') +
  theme_minimal()
</code>```

ContentLicense: CC BY-SA 4.0

Body:
I have a dataset with the following properties:

multiple locations

open for different lengths of time

with different carers present at different time points

Here's a bit of sample code:

dt <- tibble(
  "location" = c("A", "B", "C", "D", "A", "B", "C", "A", "B", "C", "A", "B", "A", "A", "A"),
  "months.since.start" = c(0,0,0,0,1,1,1,2,2,2,3,3,4,5,6),
  "carer" = c("HCA", "nurse", "nurse", "dr", "HCA", "dr", "nurse", "HCA", "dr", "nurse", "dr", "HCA", "dr", "nurse", "HCA")
)

What I would like to do is ideally 2 different things:

overlay the histogram with the counts (plot A below) in line form on
top of the histogram with the proportions (plot B below)

overlay the number with the count of active locations (the geom_text in plot A) above the proportions (in plot B)

The plots I refer to:

I'm aware that they function on different axes which is addressed in this post, but I don't need the axis of plot A to be shown, its only to show the change in active locations over time.

I suspect the first part could be done by dividing the total count by 4 (i.e the total number of active locations) and then layering the histogram on top of the proportional count, but I'm unsure how to do that.

Body:
I've managed to answer my own question - I thought I'd put it up rather than delete it, as I've not found this anywhere else (although I appreciate this is fairly simple code).

Here's the plot:

And here's the code for the above plot:

dt %>%
  ggplot(., size = 2, aes(months.since.start)) +
  geom_histogram(binwidth = 1,                  # original chart with proportions
                 position = "fill",
                 aes(fill = carer)) +
  geom_histogram(binwidth = 1,                  # the barchart with the total count
                 color = 'grey',
                 alpha = 0,                     # transparent boxes
                 aes(y=..count../4)) +          # divided by the total number of locations (4 in this case), so that it becomes a fraction of 1 and therefore will fit within the y-axis
  geom_text(stat = 'count',
            aes(label=..count..),
            position=position_fill(vjust=0.05), #the text, adjusted using position_fill, so that the position is fixed
            color = 'white') +
  theme_minimal()


问:
Finding not equal adjacent indexes in vector
I have this vector of ints:

```<code>std::vector<int> v = {1, 1, 2, 2, 2, 3};
</code>```

and I want to find indexes, where the adjacent elements aren't equal:

```<code>1
4
5
</code>```

My solution is (which isn't working):

```<code>std::vector<int> v = {1, 1, 2, 2, 2, 3};

auto i1 = std::begin(v);

while(1)
{
    i1 = std::adjacent_find(i1, std::end(v), std::not_equal_to<int>());
    std::cout << std::distance(std::begin(v), i1) << '\n';

    if (i1++ == std::end(v))
        break;
}
</code>```

This prints:

```<code>1
4
6
</code>```

The last elements points to index 6, but I want it to point 5 (value 3). Any help appreciated!

答:
Try the following approach (I renamed i1 to it)

```<code>#include <iostream>
#include <functional>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> v = { 1, 1, 2, 2, 2, 3 };

    auto it = std::begin(v);

    do
    {
        it = std::adjacent_find( it, std::end(v), std::not_equal_to<int>());
        if ( it == std::end( v ) ) it = std::prev( it );

        std::cout << std::distance(std::begin(v), it) << '\n';

        std::advance( it, 1 );
    } while ( it  != std::end( v ) );
}
</code>```

The program output is

```<code>1
4
5
</code>```

Before calling the do-while loop you should check that the vector is not empty.

For example

```<code>#include <iostream>
#include <functional>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> v = { 1, 1, 2, 2, 2, 3 };

    if ( not v.empty() )
    {
        auto it = std::begin(v);

        do
        {
            it = std::adjacent_find( it, std::end(v), std::not_equal_to<int>());
            if ( it == std::end( v ) ) it = std::prev( it );

            std::cout << std::distance(std::begin(v), it) << '\n';

            std::advance( it, 1 );
        } while ( it  != std::end( v ) );
    }
}
</code>```

As for your approach then you are trying to output

```<code>std::distance(std::begin(v), std::end( v ) )
</code>```

that is equal to 6 when i1 is equal to std::end( v ).

Body:
I have this vector of ints:

std::vector<int> v = {1, 1, 2, 2, 2, 3};

and I want to find indexes, where the adjacent elements aren't equal:

1
4
5

My solution is (which isn't working):

std::vector<int> v = {1, 1, 2, 2, 2, 3};

auto i1 = std::begin(v);

while(1)
{
    i1 = std::adjacent_find(i1, std::end(v), std::not_equal_to<int>());
    std::cout << std::distance(std::begin(v), i1) << '\n';

    if (i1++ == std::end(v))
        break;
}

This prints:

1
4
6

The last elements points to index 6, but I want it to point 5 (value 3). Any help appreciated!

Body:
Try the following approach (I renamed i1 to it)

#include <iostream>
#include <functional>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> v = { 1, 1, 2, 2, 2, 3 };

    auto it = std::begin(v);

    do
    {
        it = std::adjacent_find( it, std::end(v), std::not_equal_to<int>());
        if ( it == std::end( v ) ) it = std::prev( it );

        std::cout << std::distance(std::begin(v), it) << '\n';

        std::advance( it, 1 );
    } while ( it  != std::end( v ) );
}

The program output is

1
4
5

Before calling the do-while loop you should check that the vector is not empty.

For example

#include <iostream>
#include <functional>
#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> v = { 1, 1, 2, 2, 2, 3 };

    if ( not v.empty() )
    {
        auto it = std::begin(v);

        do
        {
            it = std::adjacent_find( it, std::end(v), std::not_equal_to<int>());
            if ( it == std::end( v ) ) it = std::prev( it );

            std::cout << std::distance(std::begin(v), it) << '\n';

            std::advance( it, 1 );
        } while ( it  != std::end( v ) );
    }
}

As for your approach then you are trying to output

std::distance(std::begin(v), std::end( v ) )

that is equal to 6 when i1 is equal to std::end( v ).


问:
Cyclic connection failure between Azure functions and MongoDB
I deployed a function in Azure that is triggered by a timer, every 5 minutes it executes a GET request to an API and stores the results into MongoDB hosted in a server.

Running the function in my localhost accessing the same MongoDB server works fine, but running the function on Azure I get a exception precisely every 10 minutes (or 2 function executions) as we can see bellow:

The exception throw is the following:

```<code>A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
</code>```

I have tried changing the MongoDB client configuration, but didn't work. Any ideias why this happens on Azure and not on the localhost emulator? And more importantly, is there any fix or work around?

Thank you!

答:
The connection you're opening here is actually a pool of 5 connections (by default) so you're right to just connect and leave it open. My guess is that you intermittently lose connectivity with mongolab and your connections die when that occurs. Hopefully, enabling auto_reconnect resolves that.

You can write a ping request to check if connection is alive or not like below.

```<code>var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
server.Ping();
</code>```

Alternatively while making connection

```<code>client = new MongoClient("mongodb://localhost");
database = client.GetDatabase(mongoDbStr);
bool isMongoLive = database.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(1000);

if(isMongoLive)
{
    // connected
}
else
{
    // couldn't connect
}
</code>```

Hope it helps.

Body:
I deployed a function in Azure that is triggered by a timer, every 5 minutes it executes a GET request to an API and stores the results into MongoDB hosted in a server.

Running the function in my localhost accessing the same MongoDB server works fine, but running the function on Azure I get a exception precisely every 10 minutes (or 2 function executions) as we can see bellow:

The exception throw is the following:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

I have tried changing the MongoDB client configuration, but didn't work. Any ideias why this happens on Azure and not on the localhost emulator? And more importantly, is there any fix or work around?

Thank you!

Body:
The connection you're opening here is actually a pool of 5 connections (by default) so you're right to just connect and leave it open. My guess is that you intermittently lose connectivity with mongolab and your connections die when that occurs. Hopefully, enabling auto_reconnect resolves that.

You can write a ping request to check if connection is alive or not like below.

var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
server.Ping();

Alternatively while making connection

client = new MongoClient("mongodb://localhost");
database = client.GetDatabase(mongoDbStr);
bool isMongoLive = database.RunCommandAsync((Command<BsonDocument>)"{ping:1}").Wait(1000);

if(isMongoLive)
{
    // connected
}
else
{
    // couldn't connect
}

Hope it helps.


问:
Resource blocked due to MIME type?
For many years I have successfully included javscript files which are dynamically created.

Here is an example:
https://granadainfo.com/sups.php?locs=95

As you can see it loads OK.

I usually put them into the head of my html document like this.

script type="text/javascript" src="https://granadainfo.com/sups.php?locs=95"

/script

In the last few days they have stopped working.

The error message from firefox debugger console is the following.

The resource from “https://granadainfo.com/sups.php?locs=95” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).

I use a2 web hosting. They must have made some change in configuration to make it stop working.

It may be that I can get over this problem with a line in the .htaccess file.

A2 hosting suggested this line but it does not work.
Header always unset X-Frame-Options

The problem only happen when there is a get request.
ie ?locs=95

There is no problem with static files.

I tried changing the file name to .js and changed the .htaccess file to parse .js as php but it makes no difference.

答:
I agree with your answer.

In the end I have changed the filenames to .js and made .js parse with php
like this in the .htacess file.

```<code>RewriteEngine on
AddHandler application/x-httpd-ea-php56 .php4 .php3 .php .phtml .htm .html .cgi .ics
.js
</code>```

Then I added this to the top of all the files

```<code>header('Content-Type: text/javascript');
</code>```

It now works.

All efforts to solve the problem with
    X-Content-Type-Options
 in the .htacess file failed.

Title: Resource blocked due to MIME type?

Body:
I agree with your answer.

In the end I have changed the filenames to .js and made .js parse with php
like this in the .htacess file.

RewriteEngine on
AddHandler application/x-httpd-ea-php56 .php4 .php3 .php .phtml .htm .html .cgi .ics
.js

Then I added this to the top of all the files

header('Content-Type: text/javascript');

It now works.

All efforts to solve the problem with
    X-Content-Type-Options
 in the .htacess file failed.


问:
What programming language would be most beneficial to have the most proficiency with?
I still have a lot to learn in terms of programming and currently only know a decent amount of C++.  I'm aware that different languages have different uses, but I was just wondering if there was perhaps one that stood out as a superior language or a language that would be applicable to more tasks?

Thanks

答: No not really. All languages have different uses and work on different platforms. Generally most OOp languages are similar in logic and only vary in syntax. Just get stuck in and you'll eventually be able to adapt to whatever language you need to use.</p>

ContentLicense: CC BY-SA 3.0


问:
deeply nested json validator using cerberus
i have a deeply nested json that i'm trying to validate with cerberus.
So, I have the following structure: (NOTE: assume even a deeper nest)

i created my schema for the first level but I cant figure out how to go deeper in the json. I can't manually write the depth schema because I don't know how many levels down the json will be.
As I understand cerberus can handle nested jsons but I can't find any code references for this exact case. Any idea how to implement this?

```<code>{
"product_number": "123",
"product_version": "ABC",
"subproducts": [
    {
        "product_number": "444",
        "product_version": "ASD",
        "subproducts": [
            {
                "product_number": "666",
                "product_version": "FFF",
                "subproducts": [
                    {
                        "product_number": "888",
                        "product_version": "JUN",
                        "subproducts": []
                    },
                    {
                        "product_number": "644",
                        "product_version": "GYB",
                        "subproducts": [
                            {
                                "product_number": "8890",
                                "product_version": "KLM",
                                "subproducts": []
                            }
                        ]
                    }
                ],
            }
        ],
    }
],}
</code>```

答:
The "$ref" keyword defined in the JSON Schema Specification may fit your needs.

```<code>{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["product_number", "product_version", "subproducts"],
    "properties": {
        "product_number": {
            "type": "string"
        },
        "product_version": {
            "type": "string"
        },
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    }
}
</code>```

If the properties "product_number" and "product_version" are required if and only if the sibling "subproducts" is not empty, the JSON schema should be modified as shown below:

```<code>{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["subproducts"],
    "properties": {
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    },
    "anyOf": [
        {
            "properties": {
                "subproducts": {
                    "maxItems": 0
                }
            }
        },
        {
            "required": ["product_number", "product_version"],
            "properties": {
                "subproducts": {
                    "minItems": 1
                },
                "product_number": {
                    "type": "string"
                },
                "product_version": {
                    "type": "string"
                }
            }
        }
    ]
}
</code>```

Body:
i have a deeply nested json that i'm trying to validate with cerberus.
So, I have the following structure: (NOTE: assume even a deeper nest)

i created my schema for the first level but I cant figure out how to go deeper in the json. I can't manually write the depth schema because I don't know how many levels down the json will be.
As I understand cerberus can handle nested jsons but I can't find any code references for this exact case. Any idea how to implement this?

{
"product_number": "123",
"product_version": "ABC",
"subproducts": [
    {
        "product_number": "444",
        "product_version": "ASD",
        "subproducts": [
            {
                "product_number": "666",
                "product_version": "FFF",
                "subproducts": [
                    {
                        "product_number": "888",
                        "product_version": "JUN",
                        "subproducts": []
                    },
                    {
                        "product_number": "644",
                        "product_version": "GYB",
                        "subproducts": [
                            {
                                "product_number": "8890",
                                "product_version": "KLM",
                                "subproducts": []
                            }
                        ]
                    }
                ],
            }
        ],
    }
],}

Body:
The "$ref" keyword defined in the JSON Schema Specification may fit your needs.

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["product_number", "product_version", "subproducts"],
    "properties": {
        "product_number": {
            "type": "string"
        },
        "product_version": {
            "type": "string"
        },
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    }
}

If the properties "product_number" and "product_version" are required if and only if the sibling "subproducts" is not empty, the JSON schema should be modified as shown below:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["subproducts"],
    "properties": {
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    },
    "anyOf": [
        {
            "properties": {
                "subproducts": {
                    "maxItems": 0
                }
            }
        },
        {
            "required": ["product_number", "product_version"],
            "properties": {
                "subproducts": {
                    "minItems": 1
                },
                "product_number": {
                    "type": "string"
                },
                "product_version": {
                    "type": "string"
                }
            }
        }
    ]
}


问:
ASP.NET MVC4 with Razor - Filtering dropdownlist by value selected from another dropdownlist
I have to filter the options of a dropdownlist by value selected from another dropdown. In my database I have a table with all countries and a table with all cities of the world with a FK to the respective country.

Here is a part of my view and my page:

And my controller methods (the GET method of the page, the loading of all countries and the loading of all cities of a country): (I removed the image)

I have to handle the "onchange" event of the first dropdownlist to modify all the options of the second (calling the LoadCities method in my controller and passing the value of the selected item of first drop) but I have no idea about how to do it.

Thank you for your help!!

UDPADE

Thank @Shyju for your advices but it still does not working. I am a student and I don't know much about the topic, here are the results:

You can see that the Content-Length is 0, in fact the response panel is empty.
Why the type is xml? What is "X-Requested-With"? How can I fix it?

答:
use javascript or jquery OnChange method.
and pass the 1st dropdown Id and use ajax to call the method by passing dropdown Id.</p>
```<code><div class="ui-widget">
        <select id="pick">
            <option value="">Select one...</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>

        <select id="drop">
            <option value="">Select one...</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
        </select>
    </div>

$("#drop").change(function () {
        var end = this.value;
        var firstDropVal = $('#pick').val();
    });
</code>```

Body:
use javascript or jquery OnChange method.
and pass the 1st dropdown Id and use ajax to call the method by passing dropdown Id.

<div class="ui-widget">
        <select id="pick">
            <option value="">Select one...</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select>

        <select id="drop">
            <option value="">Select one...</option>
            <option value="11">11</option>
            <option value="12">12</option>
            <option value="13">13</option>
            <option value="14">14</option>
        </select>
    </div>

$("#drop").change(function () {
        var end = this.value;
        var firstDropVal = $('#pick').val();
    });


问:
SQL Server - IsDate function returning unexpected result
I am trying to do something like this

```<code>when ISDATE(u.NewValue)=1 then CONVERT(varchar,cast(u.NewValue as date),23)
</code>```

But sql server returns 1 for 2406 also as it is a date, so this

```<code>select ISDATE('2406')
</code>```

returns 1.

Which is not the case.
And because of this I get the output like this 2406-01-01 which is wrong.

What should be the way to check if the value is actaully a date, when my data will be a date it will come in this format 2019-05-28 07:59:04.450, so I want to capture only this type of data as date.

Can anyone suggest me the approach for this

答:
Well, you can use like:

```<code>select (case when col like '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]'
             then convert(datetime, col)
        end)
</code>```

Depending on what your column looks like, you can relax the formatting requrements:

```<code>select (case when col like '%-%-% %:%:%.%'
             then try_convert(datetime, col)
        end)
</code>```

I am surprised that SQL Server converts this to a date just using the year:

```<code>try_convert(date, '2019', 121)
</code>```

The format can be incomplete.

Body:
I am trying to do something like this

when ISDATE(u.NewValue)=1 then CONVERT(varchar,cast(u.NewValue as date),23)

But sql server returns 1 for 2406 also as it is a date, so this

select ISDATE('2406')

returns 1.

Which is not the case.
And because of this I get the output like this 2406-01-01 which is wrong.

What should be the way to check if the value is actaully a date, when my data will be a date it will come in this format 2019-05-28 07:59:04.450, so I want to capture only this type of data as date.

Can anyone suggest me the approach for this

Body:
Well, you can use like:

select (case when col like '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]'
             then convert(datetime, col)
        end)

Depending on what your column looks like, you can relax the formatting requrements:

select (case when col like '%-%-% %:%:%.%'
             then try_convert(datetime, col)
        end)

I am surprised that SQL Server converts this to a date just using the year:

try_convert(date, '2019', 121)

The format can be incomplete.


Title: Converting array of lists to array of dictionary

Body:
I have data as follows

{"array": [[1288.1639933233453], [1255.1777295971826],
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

I want to convert above data as follows

[{"value": 1288.1639933233453},
{"value": 1255.1777295971826},
{"value": 1250.1075681742996},
{"value": 1283.9962801780075},
{"value": 1297.4411412000093}]

Body:
You can using this code:

myDic = {"array": [[1288.1639933233453], [1255.1777295971826],
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

myList = []

for i in myDic['array']:
    value = {'value':i[0]}
    myList.append(value)

print(myList)

OR

Using pythonic method :)

myDic = {"array": [[1288.1639933233453], [1255.1777295971826],
[1250.1075681742996], [1283.9962801780075], [1297.4411412000093]]}

myList = [{'value':i[0]} for i in myDic['array']]

print(myList)


问:
How to get values from JSON decoded PHP multidimmensionnal array ? (PayPal API)
I'm implementing PayPal payments in my website.

The Paypal API sends me the order details through JSON to my success.php script.

I want to get each variable sent and store it in my database.

So I get the JSON data with :

$RAW=file_get_contents('php://input');

```

<p>The data I get looks like this (print_r of $RAW) :</p>

```{"id":"XXXXXXXXXXXXX-XXXXXXXXX","event_version":"1.0","create_time":"2019-05-30T09:58:56.756Z","resource_type":"capture","resource_version":"2.0","event_type":"PAYMENT.CAPTURE.COMPLETED","summary":"Payment completed for EUR 10.0 EUR","resource":{"id":"XXXXXXXXXXXXXXXXXXX","amount":{"currency_code":"EUR","value":"10.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"EUR","value":"10.00"},"paypal_fee":{"currency_code":"EUR","value":"0.59"},"net_amount":{"currency_code":"EUR","value":"9.41"}},"status":"COMPLETED","create_time":"2019-05-30T09:58:52Z","update_time":"2019-05-30T09:58:52Z","links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXX/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXX","rel":"up","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXX-XXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX/resend","rel":"resend","method":"POST"}]}
```

<p>I decode it in an Array with :</p>

<pre class="lang-php prettyprint-override"><code>$response=json_decode($RAW, true);
</code>```

I get this array (print_r of $response) :

Array
(
    [id] => XXXXXXXXXXX-XXXXXXXXXX
    [event_version] => 1.0
    [create_time] => 2019-05-30T09:58:56.756Z
    [resource_type] => capture
    [resource_version] => 2.0
    [event_type] => PAYMENT.CAPTURE.COMPLETED
    [summary] => Payment completed for EUR 10.0 EUR
    [resource] => Array
        (
            [id] => XXXXXXXXXXXXX
            [amount] => Array
                (
                    [currency_code] => EUR
                    [value] => 10.00
                )

            [final_capture] => 1
            [seller_protection] => Array
                (
                    [status] => ELIGIBLE
                    [dispute_categories] => Array
                        (
                            [0] => ITEM_NOT_RECEIVED
                            [1] => UNAUTHORIZED_TRANSACTION
                        )

                )

            [seller_receivable_breakdown] => Array
                (
                    [gross_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 10.00
                        )

                    [paypal_fee] => Array
                        (
                            [currency_code] => EUR
                            [value] => 0.59
                        )

                    [net_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 9.41
                        )

                )

            [status] => COMPLETED
            [create_time] => 2019-05-30T09:58:52Z
            [update_time] => 2019-05-30T09:58:52Z
            [links] => Array
                (
                    [0] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXX
                            [rel] => self
                            [method] => GET
                        )

                    [1] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXX/refund
                            [rel] => refund
                            [method] => POST
                        )

                    [2] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXXX
                            [rel] => up
                            [method] => GET
                        )

                )

        )
    [links] => Array
        (
            [0] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXX-XXXXXX
                    [rel] => self
                    [method] => GET
                )

            [1] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXX-XXXXXXXXX/resend
                    [rel] => resend
                    [method] => POST
                )

        )

)
```

<p>But when I try to insert it in the database :</p>

<pre class="lang-php prettyprint-override"><code><?php
$RAW=file_get_contents('php://input');
$response=json_decode($RAW, true);

$sqluser="xxxxxxx";
$sqlpass="xxxx";
$database="xxxxxxxx";

$query="INSERT INTO testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        $response[id],
        $response[event_version],
        $response[create_time],
        $response[resource_type],
        $response[resource_version],
        $response[event_type],
        $response[summary],
        $response[resource][id],
        $response[resource][amount][currency_code],
        $response[resource][amount][value],
        $response[resource][final_capture],
        $response[resource][seller_protection][status],
        $response[resource][seller_protection][dispute_categories][0],
        $response[resource][seller_protection][dispute_categories][1],
        $response[resource][seller_receivable_breakdown][gross_amount][currency_code],
        $response[resource][seller_receivable_breakdown][gross_amount][value],
        $response[resource][seller_receivable_breakdown][paypal_fee][currency_code],
        $response[resource][seller_receivable_breakdown][paypal_fee][value],
        $response[resource][seller_receivable_breakdown][net_amount][currency_code],
        $response[resource][seller_receivable_breakdown][net_amount][value],
        $response[resource][status],
        $response[resource][create_time],
        $response[resource][update_time],
        $response[resource][links][0][href],
        $response[resource][links][0][rel],
        $response[resource][links][0][method],
        $response[resource][links][1][href],
        $response[resource][links][1][rel],
        $response[resource][links][1][method],
        $response[resource][links][2][href],
        $response[resource][links][2][rel],
        $response[resource][links][2][method],
        $response[links][0][href],
        $response[links][0][rel],
        $response[links][0][method],
        $response[links][1]href],
        $response[links][1][rel],
        $response[links][1][method]
                                        );";

try { $db = new PDO("mysql:host=sqlserver;dbname=$database", $sqluser, $sqlpass); }
catch(Exception $e) { echo 'Error : '.$e->getMessage().''; echo 'N° : '.$e->getCode(); }

$result = $db->query($query);

file_put_contents("post.log", print_r($_POST, true));
file_put_contents("get.log", print_r($_GET, true));
file_put_contents("RAW.log", print_r($RAW, true));
file_put_contents("array.log", print_r($response, true));

?>
</code>```

I get this warning :

log  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 57  PHP Notice:  Array
  to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line
  58  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 59  PHP Notice:  Array
  to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line
  60  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 61 [...]  PHP Notice:
  Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on
  line 87

And nothing is inserted.

What do i do wrong ?

答:
I found my problem :

My query declaration was missing several dots, simple & doubles quotes :

$requete="INSERT INTO xxxxxxxxx.testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        '".$response['id']."',
        '".$response['event_version']."',
        '".$response['create_time']."',
        '".$response['resource_type']."',
        '".$response['resource_version']."',
        '".$response['event_type']."',
        '".$response['summary']."',
        '".$response['resource']['id']."',
        '".$response['resource']['amount']['currency_code']."',
        '".$response['resource']['amount']['value']."',
        '".$response['resource']['final_capture']."',
        '".$response['resource']['seller_protection']['status']."',
        '".$response['resource']['seller_protection']['dispute_categories']['0']."',
        '".$response['resource']['seller_protection']['dispute_categories']['1']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['value']."',
        '".$response['resource']['status']."',
        '".$response['resource']['create_time']."',
        '".$response['resource']['update_time']."',
        '".$response['resource']['links']['0']['href']."',
        '".$response['resource']['links']['0']['rel']."',
        '".$response['resource']['links']['0']['method']."',
        '".$response['resource']['links']['1']['href']."',
        '".$response['resource']['links']['1']['rel']."',
        '".$response['resource']['links']['1']['method']."',
        '".$response['resource']['links']['2']['href']."',
        '".$response['resource']['links']['2']['rel']."',
        '".$response['resource']['links']['2']['method']."',
        '".$response['links']['0']['href']."',
        '".$response['links']['0']['rel']."',
        '".$response['links']['0']['method']."',
        '".$response['links']['1']['href']."',
        '".$response['links']['1']['rel']."',
        '".$response['links']['1']['method']."'
  );";

```

Now it works, thanks for your help !

Body:
I'm implementing PayPal payments in my website.

The Paypal API sends me the order details through JSON to my success.php script.

I want to get each variable sent and store it in my database.

So I get the JSON data with :

$RAW=file_get_contents('php://input');

The data I get looks like this (print_r of $RAW) :

{"id":"XXXXXXXXXXXXX-XXXXXXXXX","event_version":"1.0","create_time":"2019-05-30T09:58:56.756Z","resource_type":"capture","resource_version":"2.0","event_type":"PAYMENT.CAPTURE.COMPLETED","summary":"Payment completed for EUR 10.0 EUR","resource":{"id":"XXXXXXXXXXXXXXXXXXX","amount":{"currency_code":"EUR","value":"10.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"EUR","value":"10.00"},"paypal_fee":{"currency_code":"EUR","value":"0.59"},"net_amount":{"currency_code":"EUR","value":"9.41"}},"status":"COMPLETED","create_time":"2019-05-30T09:58:52Z","update_time":"2019-05-30T09:58:52Z","links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXX/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXX","rel":"up","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXX-XXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX/resend","rel":"resend","method":"POST"}]}

I decode it in an Array with :

$response=json_decode($RAW, true);

I get this array (print_r of $response) :

Array
(
    [id] => XXXXXXXXXXX-XXXXXXXXXX
    [event_version] => 1.0
    [create_time] => 2019-05-30T09:58:56.756Z
    [resource_type] => capture
    [resource_version] => 2.0
    [event_type] => PAYMENT.CAPTURE.COMPLETED
    [summary] => Payment completed for EUR 10.0 EUR
    [resource] => Array
        (
            [id] => XXXXXXXXXXXXX
            [amount] => Array
                (
                    [currency_code] => EUR
                    [value] => 10.00
                )

            [final_capture] => 1
            [seller_protection] => Array
                (
                    [status] => ELIGIBLE
                    [dispute_categories] => Array
                        (
                            [0] => ITEM_NOT_RECEIVED
                            [1] => UNAUTHORIZED_TRANSACTION
                        )

                )

            [seller_receivable_breakdown] => Array
                (
                    [gross_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 10.00
                        )

                    [paypal_fee] => Array
                        (
                            [currency_code] => EUR
                            [value] => 0.59
                        )

                    [net_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 9.41
                        )

                )

            [status] => COMPLETED
            [create_time] => 2019-05-30T09:58:52Z
            [update_time] => 2019-05-30T09:58:52Z
            [links] => Array
                (
                    [0] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXX
                            [rel] => self
                            [method] => GET
                        )

                    [1] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXX/refund
                            [rel] => refund
                            [method] => POST
                        )

                    [2] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXXX
                            [rel] => up
                            [method] => GET
                        )

                )

        )
    [links] => Array
        (
            [0] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXX-XXXXXX
                    [rel] => self
                    [method] => GET
                )

            [1] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXX-XXXXXXXXX/resend
                    [rel] => resend
                    [method] => POST
                )

        )

)

But when I try to insert it in the database :

<?php
$RAW=file_get_contents('php://input');
$response=json_decode($RAW, true);

$sqluser="xxxxxxx";
$sqlpass="xxxx";
$database="xxxxxxxx";

$query="INSERT INTO testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        $response[id],
        $response[event_version],
        $response[create_time],
        $response[resource_type],
        $response[resource_version],
        $response[event_type],
        $response[summary],
        $response[resource][id],
        $response[resource][amount][currency_code],
        $response[resource][amount][value],
        $response[resource][final_capture],
        $response[resource][seller_protection][status],
        $response[resource][seller_protection][dispute_categories][0],
        $response[resource][seller_protection][dispute_categories][1],
        $response[resource][seller_receivable_breakdown][gross_amount][currency_code],
        $response[resource][seller_receivable_breakdown][gross_amount][value],
        $response[resource][seller_receivable_breakdown][paypal_fee][currency_code],
        $response[resource][seller_receivable_breakdown][paypal_fee][value],
        $response[resource][seller_receivable_breakdown][net_amount][currency_code],
        $response[resource][seller_receivable_breakdown][net_amount][value],
        $response[resource][status],
        $response[resource][create_time],
        $response[resource][update_time],
        $response[resource][links][0][href],
        $response[resource][links][0][rel],
        $response[resource][links][0][method],
        $response[resource][links][1][href],
        $response[resource][links][1][rel],
        $response[resource][links][1][method],
        $response[resource][links][2][href],
        $response[resource][links][2][rel],
        $response[resource][links][2][method],
        $response[links][0][href],
        $response[links][0][rel],
        $response[links][0][method],
        $response[links][1]href],
        $response[links][1][rel],
        $response[links][1][method]
                                        );";

try { $db = new PDO("mysql:host=sqlserver;dbname=$database", $sqluser, $sqlpass); }
catch(Exception $e) { echo 'Error : '.$e->getMessage().''; echo 'N° : '.$e->getCode(); }

$result = $db->query($query);

file_put_contents("post.log", print_r($_POST, true));
file_put_contents("get.log", print_r($_GET, true));
file_put_contents("RAW.log", print_r($RAW, true));
file_put_contents("array.log", print_r($response, true));

?>

I get this warning :

log  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 57  PHP Notice:  Array
  to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line
  58  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 59  PHP Notice:  Array
  to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line
  60  PHP Notice:  Array to string conversion in
  /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 61 [...]  PHP Notice:
  Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on
  line 87

And nothing is inserted.

What do i do wrong ?

Body:
I found my problem :

My query declaration was missing several dots, simple & doubles quotes :

$requete="INSERT INTO xxxxxxxxx.testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        '".$response['id']."',
        '".$response['event_version']."',
        '".$response['create_time']."',
        '".$response['resource_type']."',
        '".$response['resource_version']."',
        '".$response['event_type']."',
        '".$response['summary']."',
        '".$response['resource']['id']."',
        '".$response['resource']['amount']['currency_code']."',
        '".$response['resource']['amount']['value']."',
        '".$response['resource']['final_capture']."',
        '".$response['resource']['seller_protection']['status']."',
        '".$response['resource']['seller_protection']['dispute_categories']['0']."',
        '".$response['resource']['seller_protection']['dispute_categories']['1']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['value']."',
        '".$response['resource']['status']."',
        '".$response['resource']['create_time']."',
        '".$response['resource']['update_time']."',
        '".$response['resource']['links']['0']['href']."',
        '".$response['resource']['links']['0']['rel']."',
        '".$response['resource']['links']['0']['method']."',
        '".$response['resource']['links']['1']['href']."',
        '".$response['resource']['links']['1']['rel']."',
        '".$response['resource']['links']['1']['method']."',
        '".$response['resource']['links']['2']['href']."',
        '".$response['resource']['links']['2']['rel']."',
        '".$response['resource']['links']['2']['method']."',
        '".$response['links']['0']['href']."',
        '".$response['links']['0']['rel']."',
        '".$response['links']['0']['method']."',
        '".$response['links']['1']['href']."',
        '".$response['links']['1']['rel']."',
        '".$response['links']['1']['method']."'
  );";

Now it works, thanks for your help !


问:
How to handle JS serialized item in PHP?
Hey guys!
I am having a bit of trouble handling the data from a submitted form that is serialized with JS.
In the form there a textarea fields which users can input any text they want but when they enter & like "apples & oranges", I have trouble handling that.  As we know that JS serializes & to %26 and spaces with a "+" sign.

So now back in PHP, I am trying to put this data into the database and call it back out elsewhere but for the life of me don't know how to handle the special characters properly.

Note that I am also php exploding the data at the "&" sign to make it into an array, then is put into the database as an array.  I tried several methods like urldecode, htmlentities but those don't help because remember I need to explode at "&".

I am serializing in JS because its an AJAX submitting form.

答: You can use urldecode to get the text representation of those encoded characters. I doubt that JS is doing this by itself though.</p>


问:
How to call liveData observer from another liveData observer
I am working on livedata. I have two apis but one api is dependent on another api. Based on first api response i am calling another api using livedata observer. I am calling from inside observer is this a right approach or any other alternative</p>

```<code>  mainViewModel.getListLiveData().observe(MainActivity.this, new Observer<List<Student>>() {
            @Override
            public void onChanged(@Nullable List<Student> list) {
                if(list.size() > 0){
                    mainViewModel.getStudentLiveData().observe(MainActivity.this, new Observer<Student>() {
                        @Override
                        public void onChanged(@Nullable Student student) {

                        }
                    });
                }

            }
        });
</code>```

答:
Observe on the student LiveData exposed by the mainViewModel. In the viewmodel make student live data change with change in List LiveData using Transformations or using a MediatorLiveData

In your activity:

```<code>mainViewModel.getStudentLiveData().observe(this, new Observer<Student>() {
   student -> {}
});
</code>```

In your viewmodel:

```<code>private MutableLiveData<List< Student>> studentListLiveData = new MutableLiveData(); // this will hold result of your first api call

private MutableLiveData<Student> studentLiveData = new MutableLiveData(); // this will hold result of your second api call

private void fetchData() {
    fetchStudentList(new Callback {
      result -> {
           studentListLiveData.value = result;
           fetchStudent(new Callback {
               result -> { studentLiveData.value = result; }
           });
      }
    })
}

public LiveData<Student> getStudentLiveData() {
      return studentLiveData;
}
</code>```

Body:
I am working on livedata. I have two apis but one api is dependent on another api. Based on first api response i am calling another api using livedata observer. I am calling from inside observer is this a right approach or any other alternative

  mainViewModel.getListLiveData().observe(MainActivity.this, new Observer<List<Student>>() {
            @Override
            public void onChanged(@Nullable List<Student> list) {
                if(list.size() > 0){
                    mainViewModel.getStudentLiveData().observe(MainActivity.this, new Observer<Student>() {
                        @Override
                        public void onChanged(@Nullable Student student) {

                        }
                    });
                }

            }
        });

Body:
Observe on the student LiveData exposed by the mainViewModel. In the viewmodel make student live data change with change in List LiveData using Transformations or using a MediatorLiveData

In your activity:

mainViewModel.getStudentLiveData().observe(this, new Observer<Student>() {
   student -> {}
});

In your viewmodel:

private MutableLiveData<List< Student>> studentListLiveData = new MutableLiveData(); // this will hold result of your first api call

private MutableLiveData<Student> studentLiveData = new MutableLiveData(); // this will hold result of your second api call

private void fetchData() {
    fetchStudentList(new Callback {
      result -> {
           studentListLiveData.value = result;
           fetchStudent(new Callback {
               result -> { studentLiveData.value = result; }
           });
      }
    })
}

public LiveData<Student> getStudentLiveData() {
      return studentLiveData;
}


问:
Writing all data at once to parquet file using structured streaming
I wish to write all aggregated data from a Kafka topic to a parquet file at once (or at least end up with one parquet file in the end).

I run a separate producer app that puts 50 messages on the topic.
The data is aggregated by time (1 day) in a consumer app, so I need to gather all data from 1 day and count it. This works and is done like this:

```<code>Dataset<Row> df = spark.readStream()
                .format("kafka")
                .option("kafka.bootstrap.servers", BOOTSTRAP_SERVER)
                .option("subscribe", "test")
                .option("startingOffsets", "latest")
                .option("group.id", "test")
                .option("failOnDataLoss", false)
                .option("key.deserializer", "org.apache.kafka.common.serialization.IntegerDeserializer")
                .option("value.deserializer", "org.apache.kafka.common.serialization.StringSerializer")
                .load()

// LEFT OUT CODE FOR READABILITY

                .withWatermark("timestamp", "1 minutes")
                .groupBy(
                        functions.window(new Column("timestamp"), "1 day", "1 day"),
                        new Column("container_nummer"))
                .count();
</code>```

The results are then written to a parquet file like this:

```<code>StreamingQuery query = df.writeStream()
                .format("parquet")
                .option("truncate", "false")
                .option("checkpointLocation", "/tmp/kafka-logs")
                .start("/Users/**/kafka-path");

query.awaitTermination();
</code>```

If I write this to console, I end up with the correct counts for each day in batch 1. When trying to write it to parquet, I only end up with multiple, empty parquet files. I read them like this:

```<code>SparkSession spark = SparkSession
                .builder()
                .appName("test")
                .config("spark.master", "local")
                .config("spark.sql.session.timeZone", "UTC")
                .getOrCreate();

        Dataset<Row> df = spark.read()
                .parquet("/Users/**/kafka-path/part-00000-dd416263-8db1-4166-b243-caba470adac7-c000.snappy.parquet");

        df.explain();
        df.show(20);
</code>```

all of the parquet files seem to be empty (in contrast to writing them to console), the code above outputting this:

```<code>+------+----------------+-----+
|window|container_nummer|count|
+------+----------------+-----+
+------+----------------+-----+

</code>```

I have two questions:

What is/are possible reasons that my parquet files are empty?

Is it possible to to have 1 complete parquet file with all data in it in the end? I want to use this data to feed a machine learning model in a different program.

Note: it does not need to run in production. I just hope someone knows a way for this to work..

Thanks in advance!

答:
The component involved in reading the ongoing stream is structured streaming. So, you are basically reading an unbounded stream of messages/records that will have to be written as and when the data arrives. With this as the base, Spark will keep assigning the executors to do the read/write operation and there will be multiple files that are created as a part of this process. So, you will not have one single file having all the data. Below is the syntax that can be used to write to parquet file:

```<code>df..writeStream.queryName("Loantxns_view").outputMode("append").format("parquet").option("path", "/user/root/Ln_sink2").option("checkpointLocation", "/user/root/Checkpoints2").start()
</code>```

Try to read the parquet file using the below mechanism and see if you get the data you are looking for (ParquetDF is a new dataframe that is read by reading the parquet directory path):

```<code>val ParquetDF = spark.read.parquet("/user/root/Ln_sink2")
    ParquetDF.createOrReplaceTempView("xxxx_view");
    spark.sql("select * from xxxx_view").show(false);
</code>```

Try this and see if the data is visible.

Body:
I wish to write all aggregated data from a Kafka topic to a parquet file at once (or at least end up with one parquet file in the end).

I run a separate producer app that puts 50 messages on the topic.
The data is aggregated by time (1 day) in a consumer app, so I need to gather all data from 1 day and count it. This works and is done like this:

Dataset<Row> df = spark.readStream()
                .format("kafka")
                .option("kafka.bootstrap.servers", BOOTSTRAP_SERVER)
                .option("subscribe", "test")
                .option("startingOffsets", "latest")
                .option("group.id", "test")
                .option("failOnDataLoss", false)
                .option("key.deserializer", "org.apache.kafka.common.serialization.IntegerDeserializer")
                .option("value.deserializer", "org.apache.kafka.common.serialization.StringSerializer")
                .load()

// LEFT OUT CODE FOR READABILITY

                .withWatermark("timestamp", "1 minutes")
                .groupBy(
                        functions.window(new Column("timestamp"), "1 day", "1 day"),
                        new Column("container_nummer"))
                .count();

The results are then written to a parquet file like this:

StreamingQuery query = df.writeStream()
                .format("parquet")
                .option("truncate", "false")
                .option("checkpointLocation", "/tmp/kafka-logs")
                .start("/Users/**/kafka-path");

query.awaitTermination();

If I write this to console, I end up with the correct counts for each day in batch 1. When trying to write it to parquet, I only end up with multiple, empty parquet files. I read them like this:

SparkSession spark = SparkSession
                .builder()
                .appName("test")
                .config("spark.master", "local")
                .config("spark.sql.session.timeZone", "UTC")
                .getOrCreate();

        Dataset<Row> df = spark.read()
                .parquet("/Users/**/kafka-path/part-00000-dd416263-8db1-4166-b243-caba470adac7-c000.snappy.parquet");

        df.explain();
        df.show(20);

all of the parquet files seem to be empty (in contrast to writing them to console), the code above outputting this:

+------+----------------+-----+
|window|container_nummer|count|
+------+----------------+-----+
+------+----------------+-----+

I have two questions:

What is/are possible reasons that my parquet files are empty?

Is it possible to to have 1 complete parquet file with all data in it in the end? I want to use this data to feed a machine learning model in a different program.

Note: it does not need to run in production. I just hope someone knows a way for this to work..

Thanks in advance!

Body:
The component involved in reading the ongoing stream is structured streaming. So, you are basically reading an unbounded stream of messages/records that will have to be written as and when the data arrives. With this as the base, Spark will keep assigning the executors to do the read/write operation and there will be multiple files that are created as a part of this process. So, you will not have one single file having all the data. Below is the syntax that can be used to write to parquet file:

df..writeStream.queryName("Loantxns_view").outputMode("append").format("parquet").option("path", "/user/root/Ln_sink2").option("checkpointLocation", "/user/root/Checkpoints2").start()

Try to read the parquet file using the below mechanism and see if you get the data you are looking for (ParquetDF is a new dataframe that is read by reading the parquet directory path):

val ParquetDF = spark.read.parquet("/user/root/Ln_sink2")
    ParquetDF.createOrReplaceTempView("xxxx_view");
    spark.sql("select * from xxxx_view").show(false);

Try this and see if the data is visible.


问:
Generating Indivdiual Dot Plot in R with Multipe Factors
I'm trying to generate an individual dot plot with multiple factors in R that's similar to the one produced by minitab.

I've been able to produce the individual dot plot in R but I can't figure out how to add grouping to the x-axis. For example I have a Color and a Type that I'd like to display on the x-axis, each Type has two colors and I'd like this put on the same graph but can't figure it out for a dot plot.

The result should look similar to this grouping but a dot plot instead and only have 1 graph, I'm just trying to illustrate the grouping.

http://ajpendo.physiology.org/content/280/5/E804/F6.small.gif

sorry can't post the image, too much of a noob to stack :)

I've scoured the web but can't find much help.

Any ideas?

答:
You can use ggplot to do this most directly. Let's say you have a data.frame with the following columns:

dta := x, y, color, condition

where x,y are numeric and color, condition are factors. Then you can do the following:

```<code>require(ggplot2)
ggplot(dta, aes(x = x, y = y, color = color)) + geom_point() + facet_wrap(~ condition)
</code>```

alternatively, in regular R you could do:

```<code>plot(dta$x, dta$y, col = dta$color, pch = dta$condition)
</code>```

however, this might be way too busy.

jim

Body:
You can use ggplot to do this most directly. Let's say you have a data.frame with the following columns:

dta := x, y, color, condition

where x,y are numeric and color, condition are factors. Then you can do the following:

require(ggplot2)
ggplot(dta, aes(x = x, y = y, color = color)) + geom_point() + facet_wrap(~ condition)

alternatively, in regular R you could do:

plot(dta$x, dta$y, col = dta$color, pch = dta$condition)

however, this might be way too busy.

jim


问:
Using $in clause for insensitive search in mongo db driver c#
I am wondering how to use the case insensitive for $in expressions.

According to the official MongoDB manual you can do this:

```<code>{ name: { $in: [ /^acme/i, /^ack/ ] } }
</code>```

I tested out this on Compass and it's working fine, search is insensitive.

I need to this using the Mongo Driver in C#.

I am doing this:

```<code>  var array = new BsonArray(companyNames);

  var filter = new BsonDocument { { "Name", new BsonDocument { { "$in", new BsonArray(array) }} } };
  var result = _collection.Find(filter).ToList();
</code>```

companyNames is a string[]

However this retrieves me only exact matches. It's kind of obvious because I am not including the "regex" expression. But I don't know how I am able to include the regex in a string.

The workaround is to create an $or expresion with regex for every company name.

Does anyone knows how to do this?

Thanks

答:
here's an elegant solution using a text index on the company name field.

using MongoDB.Entities;

namespace StackOverflow
{
    class Program
    {
        public class Company : Entity
        {
            public string Name { get; set; }
        }

        static void Main(string[] args)
        {
            new DB("test");

            DB.Index<Company>()
              .Key(c => c.Name, KeyType.Text)
              .Option(o => o.Background = false)
              .Create();

            var c1 = new Company { Name = "Ackme" };
            var c2 = new Company { Name = "Acme" };
            var c3 = new Company { Name = "Lackme" };
            var c4 = new Company { Name = "Hackme" };

            c1.Save(); c2.Save(); c3.Save(); c4.Save();

            var names = new[] { "ackme", "acme" };

            var result = DB.SearchText<Company>(string.Join(" ", names));
        }
    }
}
```

mind you, the above uses the convenience library MongoDB.Entities. however the concepts are the same but the syntax for the official driver is cumbersome compared to the above.

Body:
I am wondering how to use the case insensitive for $in expressions.

According to the official MongoDB manual you can do this:

{ name: { $in: [ /^acme/i, /^ack/ ] } }

I tested out this on Compass and it's working fine, search is insensitive.

I need to this using the Mongo Driver in C#.

I am doing this:

  var array = new BsonArray(companyNames);

  var filter = new BsonDocument { { "Name", new BsonDocument { { "$in", new BsonArray(array) }} } };
  var result = _collection.Find(filter).ToList();

companyNames is a string[]

However this retrieves me only exact matches. It's kind of obvious because I am not including the "regex" expression. But I don't know how I am able to include the regex in a string.

The workaround is to create an $or expresion with regex for every company name.

Does anyone knows how to do this?

Thanks

Body:
here's an elegant solution using a text index on the company name field.

using MongoDB.Entities;

namespace StackOverflow
{
    class Program
    {
        public class Company : Entity
        {
            public string Name { get; set; }
        }

        static void Main(string[] args)
        {
            new DB("test");

            DB.Index<Company>()
              .Key(c => c.Name, KeyType.Text)
              .Option(o => o.Background = false)
              .Create();

            var c1 = new Company { Name = "Ackme" };
            var c2 = new Company { Name = "Acme" };
            var c3 = new Company { Name = "Lackme" };
            var c4 = new Company { Name = "Hackme" };

            c1.Save(); c2.Save(); c3.Save(); c4.Save();

            var names = new[] { "ackme", "acme" };

            var result = DB.SearchText<Company>(string.Join(" ", names));
        }
    }
}

mind you, the above uses the convenience library MongoDB.Entities. however the concepts are the same but the syntax for the official driver is cumbersome compared to the above.


问:
how to store rich text data from CKEditor in ASP.NET Web Forms database..?
My question is very simple, please suggest me.

I am trying to create a simple blog system in ASP.Net.

I want to use CKEditor to give users the functionality of posting articles, just like wordpress.

i integrated CKEditor in my page, but having a problem.

How the data will be stores in the database, as it will be rich text data, including images, text etc. so how can it be stores in the single field in the database. and what will be the right way to retrieve it back to show as a article later.

having tough times, regarding this. I am working on ASP.Net Web Forms Technology.

tried to get reference from this post  but may be it was for ASP.Net MVC.

Body:
Just save it as HTML text in a text (or related) typed column.

Maybe you should do some sanitization before, just to check it's compliant with your rules.
CKEditor will send HTML of course but maybe someone could forge a post request sending whatever else, such as a <script> tag with some malicious code that will later be injected to your site.

If you tell me which DBMS you're using I can be more precise about colun type.


问:
How can we initialise a state in react typescript in constructor method, If it has a specific interface type
I have the below Code: Where I have an interface type for a state. How do i initialize it in constructor method. As It doesn't allow me to initialize to string/ null/ number

interface IState { selectedUser?: IUserMenu}

class AssignUser extends React.Component<IProps, IState>{
      constructor(props: IProps) {
          super(props);
          this.state = {
          selectedUser: ' what i have to put here' }}}

Also what does it mean by
this.state ={}
 in the constructor function means ?

IUserMenu is an interface, so I cannot initialize to number, '', null,

Only thing I can use Is undefined

答:
Your IState interface specifies that it has one property called selectedUser that is optional (as it has a ? after it).

If you do provide a selectedUser, it must of type IUserMenu. So your what i have to put here is an object that conforms to the IUserMenu interface.

this.state = {}  means initialise the state with the object defined in the braces. In your case the object defined in the braces must match your IState interface (so contain a single property called selectedUser that is undefined of type IUserMenu)


问:
Why is this a Not a legal OleAut date with a TimeSpan of 100 hours and 100 minutes
I'm trying to create a date from a TimeSpan that has 100 hours and 100 minutes but i'm gettin the error:

Run-time exception (line 9): Not a legal OleAut date.

Stack Trace:

[System.OverflowException: Not a legal OleAut date.]    at
  System.DateTime.TicksToOADate(Int64 value)

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);
        var OADate = DateTime.FromOADate((DateTime.MinValue.AddTicks(timeSpan.Ticks)).ToOADate());

    }
}
```

This is to link in with a legacy value in the db from vb6.

It works with small hour and minutes values but fails with this, any ideas?

Created a Fiddle

答:
Let's have a look at the date you are trying to convert into OADate:

```<code>TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);

var OADate = DateTime.MinValue.AddTicks(timeSpan.Ticks);

Console.WriteLine(OADate.ToString("d MMMM yyyy HH:mm:ss", CultureInfo.InvariantCulture));
</code>```

You'll get this

```<code>5 January 0001 05:40:00
</code>```

But according to the manual (bold is mine)

The base OLE Automation Date is midnight, 30 December 1899. The
  minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last
  moment of 31 December 9999.

that's why DateTime.MinValue.AddTicks(timeSpan.Ticks).ToOADate() is illegal (OADate is below the minimum) and you have the exception thrown.

Body:
I'm trying to create a date from a TimeSpan that has 100 hours and 100 minutes but i'm gettin the error:

Run-time exception (line 9): Not a legal OleAut date.

Stack Trace:

[System.OverflowException: Not a legal OleAut date.]    at
  System.DateTime.TicksToOADate(Int64 value)

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
        TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);
        var OADate = DateTime.FromOADate((DateTime.MinValue.AddTicks(timeSpan.Ticks)).ToOADate());

    }
}

This is to link in with a legacy value in the db from vb6.

It works with small hour and minutes values but fails with this, any ideas?

Created a Fiddle

Body:
Let's have a look at the date you are trying to convert into OADate:

TimeSpan timeSpan = TimeSpan.FromHours(100) + TimeSpan.FromMinutes(100);

var OADate = DateTime.MinValue.AddTicks(timeSpan.Ticks);

Console.WriteLine(OADate.ToString("d MMMM yyyy HH:mm:ss", CultureInfo.InvariantCulture));

You'll get this

5 January 0001 05:40:00

But according to the manual (bold is mine)

The base OLE Automation Date is midnight, 30 December 1899. The
  minimum OLE Automation date is midnight, 1 January 0100. The maximum OLE Automation Date is the same as DateTime.MaxValue, the last
  moment of 31 December 9999.

that's why DateTime.MinValue.AddTicks(timeSpan.Ticks).ToOADate() is illegal (OADate is below the minimum) and you have the exception thrown.


问:
Google Annotation Chart load from CSV
I am trying to create a Google Annotation chart by loading some data from a CSV file using the example found here:

https://developers.google.com/chart/interactive/docs/gallery/annotationchart

I've tried to modify the code (using my limited JS knowledge) to load from a CSV file but I'm getting no graph.

My code so far:

```<code><html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type='text/javascript'>
google.charts.load('current', {'packages':['annotationchart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart()
{
$.get('test.csv', function(csvString)
{
var arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
arrayData = arrayData.map(function (row)
{
return
[new Date(row[0]),row[1]];
});

var data = google.visualization.arrayToDataTable(arrayData);
var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));

var options = {
displayAnnotations: true
};

chart.draw(data, options);
}
}
</script>
</head>

<body>
<div id='chart_div' style='width: 900px; height: 500px;'></div>
</body>
</html>
</code>```

CSV File

```<code>Date,Value1
2014-01-01,1233
2014- 01-02,1334
2014-01-03,1488
2014-01-04,1888
2014-01-05,2011
2014-01-06,1900
2014-01-07,1768
2014-01-08,2345
</code>```

答:
first, add jquery and jquery csv to your page.

```<code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>
</code>```

then replace the code as follows.

see comments for explanations...

// load google charts
google.charts.load('current', {
  packages: ['annotationchart']
}).then(function () {
  // declare data variable
  var arrayData;

  // get csv data
  $.get('test.csv', function(csvString) {
    // get csv data success, convert to an array, draw chart
    arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
    drawChart(arrayData);
  }).fail(function () {
    // get csv data failed, draw chart with hard-coded data, for example purposes
    arrayData = [
      ['Date','Value1'],
      ['2014-01-01',1233],
      ['2014-01-02',1334],
      ['2014-01-03',1488],
      ['2014-01-04',1888],
      ['2014-01-05',2011],
      ['2014-01-06',1900],
      ['2014-01-07',1768],
      ['2014-01-08',2345],
    ];
    drawChart(arrayData);
  });
});

// draw chart
function drawChart(arrayData) {
  // convert string in first column to a date
  arrayData = arrayData.map(function (row) {
    return [new Date(row[0]),row[1]];
  });

  // create google data table, chart, and options
  var data = google.visualization.arrayToDataTable(arrayData);
  var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
  var options = {
    displayAnnotations: true
  };

  // draw chart
  chart.draw(data, options);
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div></code>```

note: you can remove the fail callback, it is for example purposes here on stack overflow...

Title: Google Annotation Chart load from CSV

Body:
I am trying to create a Google Annotation chart by loading some data from a CSV file using the example found here:

https://developers.google.com/chart/interactive/docs/gallery/annotationchart

I've tried to modify the code (using my limited JS knowledge) to load from a CSV file but I'm getting no graph.

My code so far:

CSV File

Date,Value1
2014-01-01,1233
2014- 01-02,1334
2014-01-03,1488
2014-01-04,1888
2014-01-05,2011
2014-01-06,1900
2014-01-07,1768
2014-01-08,2345

Body:
first, add jquery and jquery csv to your page.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>

then replace the code as follows.

see comments for explanations...

// load google charts
google.charts.load('current', {
  packages: ['annotationchart']
}).then(function () {
  // declare data variable
  var arrayData;

  // get csv data
  $.get('test.csv', function(csvString) {
    // get csv data success, convert to an array, draw chart
    arrayData = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
    drawChart(arrayData);
  }).fail(function () {
    // get csv data failed, draw chart with hard-coded data, for example purposes
    arrayData = [
      ['Date','Value1'],
      ['2014-01-01',1233],
      ['2014-01-02',1334],
      ['2014-01-03',1488],
      ['2014-01-04',1888],
      ['2014-01-05',2011],
      ['2014-01-06',1900],
      ['2014-01-07',1768],
      ['2014-01-08',2345],
    ];
    drawChart(arrayData);
  });
});

// draw chart
function drawChart(arrayData) {
  // convert string in first column to a date
  arrayData = arrayData.map(function (row) {
    return [new Date(row[0]),row[1]];
  });

  // create google data table, chart, and options
  var data = google.visualization.arrayToDataTable(arrayData);
  var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
  var options = {
    displayAnnotations: true
  };

  // draw chart
  chart.draw(data, options);
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/0.71/jquery.csv-0.71.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

note: you can remove the fail callback, it is for example purposes here on stack overflow...


问:
Hadoop gzip compressed files
I am new to hadoop and trying to process wikipedia dump. It's a 6.7 GB gzip compressed xml file. I read that hadoop supports gzip compressed files but can only be processed by mapper on a single job as only one mapper can decompress it. This seems to put a limitation on the processing. Is there an alternative? like decompressing and splitting the xml file into multiple chunks and recompressing them with gzip.

I read about the hadoop gzip from http://researchcomputing.blogspot.com/2008/04/hadoop-and-compressed-files.html

Thanks for your help.

答:
GZIP files cannot be partitioned in any way, due to a limitation of the codec.  6.7GB really isn't that big, so just decompress it on a single machine (it will take less than an hour) and copy the XML up to HDFS.  Then you can process the Wikipedia XML in Hadoop.

Cloud9 contains a WikipediaPageInputFormat class that you can use to read the XML in Hadoop.

Title: Hadoop gzip compressed files


问:
my smart contract cause error : out of gas error

i have some problem in my solidity code.
i think, the problem cause on the account.transfer

i'm using ganache, remix, metamask, web3

this is my solidity code.

```<code>`pragma solidity ^0.4.25;
contract test{
    address fire_account=0x6F3c5e42c340736eEa9a1C362592Bef9Ba2E5561;
    mapping(string => uint) fire_record;
    function fireDonation(string contributorName) payable{
        fire_account.transfer(msg.value);
        fire_balance+=msg.value;
        fire_contributor.push(contributorName);
    }
}`
</code>```

and web3.js code

```<code>var Courses;
async function init(){
    web3 = await new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    web3.eth.defaultAccount = web3.eth.accounts[2];
    console.log("Default account is ",web3.eth.accounts[2]);
    //Change the ABI here
    var CoursesContract = web3.eth.contract(/*skip*/);
    //Replace Deployed Smartcontract Address here
    var contractaddress = "0x9b95f972feaad42f2023246112f450c56d8921ae";
    Courses = CoursesContract.at(contractaddress);
}
init();
function fireDonate(){
    var amount=parseInt(document.getElementById('amount').value);
    console.log(amount);
    Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount});
}
</code>```

can u tell me solution or link?
i can't find links..

答:
It appears that your smart contract requires more gas (fees) to execute than the default amount. You have to increase the gasLimit and gasPrice from their default values. You can specify them in hex like this. </p>

```<code>Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount, gasLimit: '0x27100', gasPrice: '0x09184e72a00'});
</code>```

<p>You might have to vary the exact values to see what works for your use case. </p>

Body:
i have some problem in my solidity code.
i think, the problem cause on the account.transfer

i'm using ganache, remix, metamask, web3

this is my solidity code.

`pragma solidity ^0.4.25;
contract test{
    address fire_account=0x6F3c5e42c340736eEa9a1C362592Bef9Ba2E5561;
    mapping(string => uint) fire_record;
    function fireDonation(string contributorName) payable{
        fire_account.transfer(msg.value);
        fire_balance+=msg.value;
        fire_contributor.push(contributorName);
    }
}`

and web3.js code

var Courses;
async function init(){
    web3 = await new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
    web3.eth.defaultAccount = web3.eth.accounts[2];
    console.log("Default account is ",web3.eth.accounts[2]);
    //Change the ABI here
    var CoursesContract = web3.eth.contract(/*skip*/);
    //Replace Deployed Smartcontract Address here
    var contractaddress = "0x9b95f972feaad42f2023246112f450c56d8921ae";
    Courses = CoursesContract.at(contractaddress);
}
init();
function fireDonate(){
    var amount=parseInt(document.getElementById('amount').value);
    console.log(amount);
    Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount});
}

can u tell me solution or link?
i can't find links..

Body:
It appears that your smart contract requires more gas (fees) to execute than the default amount. You have to increase the gasLimit and gasPrice from their default values. You can specify them in hex like this.

Courses.fireDonation(document.getElementById('contributor').value, {from: web3.eth.accounts[2], value: amount, gasLimit: '0x27100', gasPrice: '0x09184e72a00'});

You might have to vary the exact values to see what works for your use case.


问:
Removing the duplicates from an array of objects
Duplicate values are being storing into an array of object

Based on the given userID need to out the duplicate userId,
if it exist do nothing, else push that userIdinto the teamSocketsList array

But with the below piece of code duplicate values are being stored into an array teamSocketsList

```<code>var  TeamUser = {
    userId : userID,
    socketId : socket.id
}

var i = $.inArray( userID, teamSocketsList );
if(i == -1){
    teamSocketsList.push(TeamUser);
}else{
        teamSocketsList = jQuery.grep(teamSocketsList, function(value) {
        return value != userID;
    });
}
</code>```

Actual Result:

```<code>[
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":1,"socketId":"ZIbgYMLOda_R5QqvAAAN"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"}
]
</code>```

Expected Result:

```<code>[
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
]
</code>```

Edit:

Here I'm expecting userID to be pushed in to an array teamSocketsList based on the condition, if at all the given userID matches in the present list should return false or do nothing. Otherwise, (iff, it's not at all included then) store it into an array with the auto-generated socketId value

The logic which I had tried to implement was to check whether if that array is empty or not and then, traverse all the elements in the array list so that whenever userID were given as an input it must check the condition and then push that element.

Based on the present answers put up, it'll store the duplicate values and then sort it on and assign it back to teamSocketsList, that's fine.

答:
Along with already mentioned values/reduce, you can take advantage of Set():

const myArray = [
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":1,"socketId":"ZIbgYMLOda_R5QqvAAAN"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"}
]

const unique = (arr, key) => {
   const keys = new Set();
   return arr.filter(el => !keys.has(el[key]) && keys.add(el[key]));
 };

console.log(unique(myArray, "userId"));```

In opposition to other response I can point out that with this method is easier to change array and key without adding code.

Body:
Duplicate values are being storing into an array of object

Based on the given userID need to out the duplicate userId,
if it exist do nothing, else push that userIdinto the teamSocketsList array

But with the below piece of code duplicate values are being stored into an array teamSocketsList

var  TeamUser = {
    userId : userID,
    socketId : socket.id
}

var i = $.inArray( userID, teamSocketsList );
if(i == -1){
    teamSocketsList.push(TeamUser);
}else{
        teamSocketsList = jQuery.grep(teamSocketsList, function(value) {
        return value != userID;
    });
}

Actual Result:

[
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":1,"socketId":"ZIbgYMLOda_R5QqvAAAN"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"}
]

Expected Result:

[
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
]

Edit:

Here I'm expecting userID to be pushed in to an array teamSocketsList based on the condition, if at all the given userID matches in the present list should return false or do nothing. Otherwise, (iff, it's not at all included then) store it into an array with the auto-generated socketId value

The logic which I had tried to implement was to check whether if that array is empty or not and then, traverse all the elements in the array list so that whenever userID were given as an input it must check the condition and then push that element.

Based on the present answers put up, it'll store the duplicate values and then sort it on and assign it back to teamSocketsList, that's fine.

Body:
Along with already mentioned values/reduce, you can take advantage of Set():

const myArray = [
    {"userId":1,"socketId":"M8xzpi3O0cMHXe-dAAAK"},
    {"userId":1,"socketId":"ZIbgYMLOda_R5QqvAAAN"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"},
    {"userId":9,"socketId":"XAf1cepsLv-KDpn3AAAQ"}
]

const unique = (arr, key) => {
   const keys = new Set();
   return arr.filter(el => !keys.has(el[key]) && keys.add(el[key]));
 };

console.log(unique(myArray, "userId"));

In opposition to other response I can point out that with this method is easier to change array and key without adding code.