Each version is correct, but each expresses a slightly different meaning.
"It's dangerous to walk on the road" might be used to indicate a hazard associated with the road in general, such as the existence of gangs of thieves who rob pedestrians.
"It's dangerous to walk in the road" is somewhat more specific, suggesting a distinction between walking along the edge of the road vs walking in the middle. It also has associations with related phrases such as walking in traffic.
So it would be dangerous to walk ON the road because you might get robbed, or struck by lightning during a storm, etc, while it is dangerous to walk IN the road because you might get hit by a car.